blob: 24304300d7b57b1fa359cd73660636590702e1b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andrew Vasquezfa90c542005-10-27 11:10:08 -07002 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include "qla_def.h"
8
9#include <linux/moduleparam.h>
10#include <linux/vmalloc.h>
11#include <linux/smp_lock.h>
12#include <linux/delay.h>
13
14#include <scsi/scsi_tcq.h>
15#include <scsi/scsicam.h>
16#include <scsi/scsi_transport.h>
17#include <scsi/scsi_transport_fc.h>
18
19/*
20 * Driver version
21 */
22char qla2x00_version_str[40];
23
24/*
25 * SRB allocation cache
26 */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -040027static kmem_cache_t *srb_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * Ioctl related information.
31 */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -040032static int num_hosts;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34int ql2xlogintimeout = 20;
35module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
36MODULE_PARM_DESC(ql2xlogintimeout,
37 "Login timeout value in seconds.");
38
8482e1182005-04-17 15:04:54 -050039int qlport_down_retry = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
41MODULE_PARM_DESC(qlport_down_retry,
42 "Maximum number of command retries to a port that returns"
43 "a PORT-DOWN status.");
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045int ql2xplogiabsentdevice;
46module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
47MODULE_PARM_DESC(ql2xplogiabsentdevice,
48 "Option to enable PLOGI to devices that are not present after "
49 "a Fabric scan. This is needed for several broken switches."
50 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052int ql2xloginretrycount = 0;
53module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
54MODULE_PARM_DESC(ql2xloginretrycount,
55 "Specify an alternate value for the NVRAM login retry count.");
56
Andrew Vasquez331e3472005-11-09 15:49:19 -080057#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
58int ql2xfwloadflash;
59module_param(ql2xfwloadflash, int, S_IRUGO|S_IRUSR);
60MODULE_PARM_DESC(ql2xfwloadflash,
61 "Load ISP24xx firmware image from FLASH (onboard memory).");
62#endif
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void qla2x00_free_device(scsi_qla_host_t *);
65
66static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
67
Andrew Vasquezcca53352005-08-26 19:08:30 -070068int ql2xfdmienable;
69module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
70MODULE_PARM_DESC(ql2xfdmienable,
71 "Enables FDMI registratons "
72 "Default is 0 - no FDMI. 1 - perfom FDMI.");
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -070075 * SCSI host template entry points
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
77static int qla2xxx_slave_configure(struct scsi_device * device);
f4f051e2005-04-17 15:02:26 -050078static int qla2xxx_slave_alloc(struct scsi_device *);
79static void qla2xxx_slave_destroy(struct scsi_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
81 void (*fn)(struct scsi_cmnd *));
Andrew Vasquezfca29702005-07-06 10:31:47 -070082static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
83 void (*fn)(struct scsi_cmnd *));
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int qla2xxx_eh_abort(struct scsi_cmnd *);
85static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
86static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
87static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
88static int qla2x00_loop_reset(scsi_qla_host_t *ha);
89static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
90
Andrew Vasquezce7e4af2005-08-26 19:09:30 -070091static int qla2x00_change_queue_depth(struct scsi_device *, int);
92static int qla2x00_change_queue_type(struct scsi_device *, int);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static struct scsi_host_template qla2x00_driver_template = {
95 .module = THIS_MODULE,
96 .name = "qla2xxx",
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .queuecommand = qla2x00_queuecommand,
98
99 .eh_abort_handler = qla2xxx_eh_abort,
100 .eh_device_reset_handler = qla2xxx_eh_device_reset,
101 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
102 .eh_host_reset_handler = qla2xxx_eh_host_reset,
103
104 .slave_configure = qla2xxx_slave_configure,
105
f4f051e2005-04-17 15:02:26 -0500106 .slave_alloc = qla2xxx_slave_alloc,
107 .slave_destroy = qla2xxx_slave_destroy,
Andrew Vasquezce7e4af2005-08-26 19:09:30 -0700108 .change_queue_depth = qla2x00_change_queue_depth,
109 .change_queue_type = qla2x00_change_queue_type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .this_id = -1,
111 .cmd_per_lun = 3,
112 .use_clustering = ENABLE_CLUSTERING,
113 .sg_tablesize = SG_ALL,
114
115 /*
116 * The RISC allows for each command to transfer (2^32-1) bytes of data,
117 * which equates to 0x800000 sectors.
118 */
119 .max_sectors = 0xFFFF,
Andrew Vasquezafb046e2005-08-26 19:09:40 -0700120 .shost_attrs = qla2x00_host_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Andrew Vasquezfca29702005-07-06 10:31:47 -0700123static struct scsi_host_template qla24xx_driver_template = {
124 .module = THIS_MODULE,
125 .name = "qla2xxx",
126 .queuecommand = qla24xx_queuecommand,
127
128 .eh_abort_handler = qla2xxx_eh_abort,
129 .eh_device_reset_handler = qla2xxx_eh_device_reset,
130 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
131 .eh_host_reset_handler = qla2xxx_eh_host_reset,
132
133 .slave_configure = qla2xxx_slave_configure,
134
135 .slave_alloc = qla2xxx_slave_alloc,
136 .slave_destroy = qla2xxx_slave_destroy,
Andrew Vasquezce7e4af2005-08-26 19:09:30 -0700137 .change_queue_depth = qla2x00_change_queue_depth,
138 .change_queue_type = qla2x00_change_queue_type,
Andrew Vasquezfca29702005-07-06 10:31:47 -0700139 .this_id = -1,
140 .cmd_per_lun = 3,
141 .use_clustering = ENABLE_CLUSTERING,
142 .sg_tablesize = SG_ALL,
143
144 .max_sectors = 0xFFFF,
Andrew Vasquezafb046e2005-08-26 19:09:40 -0700145 .shost_attrs = qla2x00_host_attrs,
Andrew Vasquezfca29702005-07-06 10:31:47 -0700146};
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static struct scsi_transport_template *qla2xxx_transport_template = NULL;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* TODO Convert to inlines
151 *
152 * Timer routines
153 */
154#define WATCH_INTERVAL 1 /* number of seconds */
155
156static void qla2x00_timer(scsi_qla_host_t *);
157
158static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
159 void *, unsigned long);
160static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
161static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
162
163static inline void
164qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
165{
166 init_timer(&ha->timer);
167 ha->timer.expires = jiffies + interval * HZ;
168 ha->timer.data = (unsigned long)ha;
169 ha->timer.function = (void (*)(unsigned long))func;
170 add_timer(&ha->timer);
171 ha->timer_active = 1;
172}
173
174static inline void
175qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
176{
177 mod_timer(&ha->timer, jiffies + interval * HZ);
178}
179
180static __inline__ void
181qla2x00_stop_timer(scsi_qla_host_t *ha)
182{
183 del_timer_sync(&ha->timer);
184 ha->timer_active = 0;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int qla2x00_do_dpc(void *data);
188
189static void qla2x00_rst_aen(scsi_qla_host_t *);
190
191static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
192static void qla2x00_mem_free(scsi_qla_host_t *ha);
193static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
194static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
f4f051e2005-04-17 15:02:26 -0500195static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
196void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* -------------------------------------------------------------------------- */
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static char *
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700201qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 static char *pci_bus_modes[] = {
204 "33", "66", "100", "133",
205 };
206 uint16_t pci_bus;
207
208 strcpy(str, "PCI");
209 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
210 if (pci_bus) {
211 strcat(str, "-X (");
212 strcat(str, pci_bus_modes[pci_bus]);
213 } else {
214 pci_bus = (ha->pci_attr & BIT_8) >> 8;
215 strcat(str, " (");
216 strcat(str, pci_bus_modes[pci_bus]);
217 }
218 strcat(str, " MHz)");
219
220 return (str);
221}
222
Andrew Vasquezfca29702005-07-06 10:31:47 -0700223static char *
224qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
225{
226 static char *pci_bus_modes[] = { "33", "66", "100", "133", };
227 uint32_t pci_bus;
228 int pcie_reg;
229
230 pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
231 if (pcie_reg) {
232 char lwstr[6];
233 uint16_t pcie_lstat, lspeed, lwidth;
234
235 pcie_reg += 0x12;
236 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
237 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
238 lwidth = (pcie_lstat &
239 (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
240
241 strcpy(str, "PCIe (");
242 if (lspeed == 1)
243 strcat(str, "2.5Gb/s ");
244 else
245 strcat(str, "<unknown> ");
246 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
247 strcat(str, lwstr);
248
249 return str;
250 }
251
252 strcpy(str, "PCI");
253 pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
254 if (pci_bus == 0 || pci_bus == 8) {
255 strcat(str, " (");
256 strcat(str, pci_bus_modes[pci_bus >> 3]);
257 } else {
258 strcat(str, "-X ");
259 if (pci_bus & BIT_2)
260 strcat(str, "Mode 2");
261 else
262 strcat(str, "Mode 1");
263 strcat(str, " (");
264 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
265 }
266 strcat(str, " MHz)");
267
268 return str;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271char *
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700272qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 char un_str[10];
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
277 ha->fw_minor_version,
278 ha->fw_subminor_version);
279
280 if (ha->fw_attributes & BIT_9) {
281 strcat(str, "FLX");
282 return (str);
283 }
284
285 switch (ha->fw_attributes & 0xFF) {
286 case 0x7:
287 strcat(str, "EF");
288 break;
289 case 0x17:
290 strcat(str, "TP");
291 break;
292 case 0x37:
293 strcat(str, "IP");
294 break;
295 case 0x77:
296 strcat(str, "VI");
297 break;
298 default:
299 sprintf(un_str, "(%x)", ha->fw_attributes);
300 strcat(str, un_str);
301 break;
302 }
303 if (ha->fw_attributes & 0x100)
304 strcat(str, "X");
305
306 return (str);
307}
308
Andrew Vasquezfca29702005-07-06 10:31:47 -0700309char *
310qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
311{
Andrew Vasquezf0883ac2005-07-08 17:58:43 -0700312 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
313 ha->fw_minor_version,
314 ha->fw_subminor_version);
315
Andrew Vasquezfca29702005-07-06 10:31:47 -0700316 if (ha->fw_attributes & BIT_0)
317 strcat(str, "[Class 2] ");
318 if (ha->fw_attributes & BIT_1)
319 strcat(str, "[IP] ");
320 if (ha->fw_attributes & BIT_2)
321 strcat(str, "[Multi-ID] ");
322 if (ha->fw_attributes & BIT_13)
323 strcat(str, "[Experimental]");
324 return str;
Andrew Vasquezfca29702005-07-06 10:31:47 -0700325}
326
327static inline srb_t *
328qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
329 struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
330{
331 srb_t *sp;
332
333 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
334 if (!sp)
335 return sp;
336
337 atomic_set(&sp->ref_count, 1);
338 sp->ha = ha;
339 sp->fcport = fcport;
340 sp->cmd = cmd;
341 sp->flags = 0;
342 CMD_SP(cmd) = (void *)sp;
343 cmd->scsi_done = done;
344
345 return sp;
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static int
f4f051e2005-04-17 15:02:26 -0500349qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
f4f051e2005-04-17 15:02:26 -0500351 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500352 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400353 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
f4f051e2005-04-17 15:02:26 -0500354 srb_t *sp;
355 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400357 rval = fc_remote_port_chkready(rport);
358 if (rval) {
359 cmd->result = rval;
f4f051e2005-04-17 15:02:26 -0500360 goto qc_fail_command;
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
f4f051e2005-04-17 15:02:26 -0500363 if (atomic_read(&fcport->state) != FCS_ONLINE) {
364 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
365 atomic_read(&ha->loop_state) == LOOP_DEAD) {
366 cmd->result = DID_NO_CONNECT << 16;
367 goto qc_fail_command;
368 }
369 goto qc_host_busy;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 spin_unlock_irq(ha->host->host_lock);
373
Andrew Vasquezfca29702005-07-06 10:31:47 -0700374 sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
375 if (!sp)
f4f051e2005-04-17 15:02:26 -0500376 goto qc_host_busy_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
f4f051e2005-04-17 15:02:26 -0500378 rval = qla2x00_start_scsi(sp);
379 if (rval != QLA_SUCCESS)
380 goto qc_host_busy_free_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock_irq(ha->host->host_lock);
383
f4f051e2005-04-17 15:02:26 -0500384 return 0;
385
386qc_host_busy_free_sp:
387 qla2x00_sp_free_dma(ha, sp);
f4f051e2005-04-17 15:02:26 -0500388 mempool_free(sp, ha->srb_mempool);
389
390qc_host_busy_lock:
391 spin_lock_irq(ha->host->host_lock);
392
393qc_host_busy:
394 return SCSI_MLQUEUE_HOST_BUSY;
395
396qc_fail_command:
397 done(cmd);
398
399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Andrew Vasquezfca29702005-07-06 10:31:47 -0700402
403static int
404qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
405{
406 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
407 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400408 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
Andrew Vasquezfca29702005-07-06 10:31:47 -0700409 srb_t *sp;
410 int rval;
411
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400412 rval = fc_remote_port_chkready(rport);
413 if (rval) {
414 cmd->result = rval;
Andrew Vasquezfca29702005-07-06 10:31:47 -0700415 goto qc24_fail_command;
416 }
417
418 if (atomic_read(&fcport->state) != FCS_ONLINE) {
419 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
420 atomic_read(&ha->loop_state) == LOOP_DEAD) {
421 cmd->result = DID_NO_CONNECT << 16;
422 goto qc24_fail_command;
423 }
424 goto qc24_host_busy;
425 }
426
427 spin_unlock_irq(ha->host->host_lock);
428
429 sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
430 if (!sp)
431 goto qc24_host_busy_lock;
432
433 rval = qla24xx_start_scsi(sp);
434 if (rval != QLA_SUCCESS)
435 goto qc24_host_busy_free_sp;
436
437 spin_lock_irq(ha->host->host_lock);
438
439 return 0;
440
441qc24_host_busy_free_sp:
442 qla2x00_sp_free_dma(ha, sp);
443 mempool_free(sp, ha->srb_mempool);
444
445qc24_host_busy_lock:
446 spin_lock_irq(ha->host->host_lock);
447
448qc24_host_busy:
449 return SCSI_MLQUEUE_HOST_BUSY;
450
451qc24_fail_command:
452 done(cmd);
453
454 return 0;
455}
456
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * qla2x00_eh_wait_on_command
460 * Waits for the command to be returned by the Firmware for some
461 * max time.
462 *
463 * Input:
464 * ha = actual ha whose done queue will contain the command
465 * returned by firmware.
466 * cmd = Scsi Command to wait on.
467 * flag = Abort/Reset(Bus or Device Reset)
468 *
469 * Return:
470 * Not Found : 0
471 * Found : 1
472 */
473static int
474qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
475{
Andrew Vasquezfe74c712005-08-26 19:10:10 -0700476#define ABORT_POLLING_PERIOD 1000
477#define ABORT_WAIT_ITER ((10 * 1000) / (ABORT_POLLING_PERIOD))
f4f051e2005-04-17 15:02:26 -0500478 unsigned long wait_iter = ABORT_WAIT_ITER;
479 int ret = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
f4f051e2005-04-17 15:02:26 -0500481 while (CMD_SP(cmd)) {
Andrew Vasquezfe74c712005-08-26 19:10:10 -0700482 msleep(ABORT_POLLING_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
f4f051e2005-04-17 15:02:26 -0500484 if (--wait_iter)
485 break;
486 }
487 if (CMD_SP(cmd))
488 ret = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
f4f051e2005-04-17 15:02:26 -0500490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/*
494 * qla2x00_wait_for_hba_online
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700495 * Wait till the HBA is online after going through
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * <= MAX_RETRIES_OF_ISP_ABORT or
497 * finally HBA is disabled ie marked offline
498 *
499 * Input:
500 * ha - pointer to host adapter structure
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700501 *
502 * Note:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * Does context switching-Release SPIN_LOCK
504 * (if any) before calling this routine.
505 *
506 * Return:
507 * Success (Adapter is online) : 0
508 * Failed (Adapter is offline/disabled) : 1
509 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700510static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
512{
Andrew Vasquezfca29702005-07-06 10:31:47 -0700513 int return_status;
514 unsigned long wait_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700516 wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
518 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
519 test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
520 ha->dpc_active) && time_before(jiffies, wait_online)) {
521
522 msleep(1000);
523 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700524 if (ha->flags.online)
525 return_status = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 else
527 return_status = QLA_FUNCTION_FAILED;
528
529 DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
530
531 return (return_status);
532}
533
534/*
535 * qla2x00_wait_for_loop_ready
536 * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700537 * to be in LOOP_READY state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Input:
539 * ha - pointer to host adapter structure
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700540 *
541 * Note:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * Does context switching-Release SPIN_LOCK
543 * (if any) before calling this routine.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700544 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
546 * Return:
547 * Success (LOOP_READY) : 0
548 * Failed (LOOP_NOT_READY) : 1
549 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700550static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
552{
553 int return_status = QLA_SUCCESS;
554 unsigned long loop_timeout ;
555
556 /* wait for 5 min at the max for loop to be ready */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700557 loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 while ((!atomic_read(&ha->loop_down_timer) &&
560 atomic_read(&ha->loop_state) == LOOP_DOWN) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 atomic_read(&ha->loop_state) != LOOP_READY) {
562 msleep(1000);
563 if (time_after_eq(jiffies, loop_timeout)) {
564 return_status = QLA_FUNCTION_FAILED;
565 break;
566 }
567 }
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700568 return (return_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/**************************************************************************
572* qla2xxx_eh_abort
573*
574* Description:
575* The abort function will abort the specified command.
576*
577* Input:
578* cmd = Linux SCSI command packet to be aborted.
579*
580* Returns:
581* Either SUCCESS or FAILED.
582*
583* Note:
584**************************************************************************/
585int
586qla2xxx_eh_abort(struct scsi_cmnd *cmd)
587{
f4f051e2005-04-17 15:02:26 -0500588 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
589 srb_t *sp;
590 int ret, i;
591 unsigned int id, lun;
592 unsigned long serial;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700593 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
f4f051e2005-04-17 15:02:26 -0500595 if (!CMD_SP(cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
f4f051e2005-04-17 15:02:26 -0500598 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
f4f051e2005-04-17 15:02:26 -0500600 id = cmd->device->id;
601 lun = cmd->device->lun;
602 serial = cmd->serial_number;
603
604 /* Check active list for command command. */
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700605 spin_lock_irqsave(&ha->hardware_lock, flags);
f4f051e2005-04-17 15:02:26 -0500606 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
607 sp = ha->outstanding_cmds[i];
608
609 if (sp == NULL)
610 continue;
611
612 if (sp->cmd != cmd)
613 continue;
614
615 DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
616 "sp->state=%x\n", __func__, ha->host_no, sp, serial,
617 sp->state));
618 DEBUG3(qla2x00_print_scsi_cmd(cmd);)
619
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700620 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700621 if (ha->isp_ops.abort_command(ha, sp)) {
f4f051e2005-04-17 15:02:26 -0500622 DEBUG2(printk("%s(%ld): abort_command "
623 "mbx failed.\n", __func__, ha->host_no));
624 } else {
625 DEBUG3(printk("%s(%ld): abort_command "
626 "mbx success.\n", __func__, ha->host_no));
627 ret = SUCCESS;
628 }
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700629 spin_lock_irqsave(&ha->hardware_lock, flags);
f4f051e2005-04-17 15:02:26 -0500630
631 break;
632 }
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700633 spin_unlock_irqrestore(&ha->hardware_lock, flags);
f4f051e2005-04-17 15:02:26 -0500634
635 /* Wait for the command to be returned. */
636 if (ret == SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500637 if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700638 qla_printk(KERN_ERR, ha,
f4f051e2005-04-17 15:02:26 -0500639 "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
640 "%x.\n", ha->host_no, id, lun, serial, ret);
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700644 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500645 "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
646 id, lun, serial, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
f4f051e2005-04-17 15:02:26 -0500648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/**************************************************************************
652* qla2x00_eh_wait_for_pending_target_commands
653*
654* Description:
655* Waits for all the commands to come back from the specified target.
656*
657* Input:
658* ha - pointer to scsi_qla_host structure.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700659* t - target
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660* Returns:
661* Either SUCCESS or FAILED.
662*
663* Note:
664**************************************************************************/
665static int
666qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
667{
668 int cnt;
669 int status;
670 srb_t *sp;
671 struct scsi_cmnd *cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700672 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 status = 0;
675
676 /*
677 * Waiting for all commands for the designated target in the active
678 * array
679 */
680 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700681 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 sp = ha->outstanding_cmds[cnt];
683 if (sp) {
684 cmd = sp->cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700685 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (cmd->device->id == t) {
687 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
688 status = 1;
689 break;
690 }
691 }
f4f051e2005-04-17 15:02:26 -0500692 } else {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700693 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 }
696 return (status);
697}
698
699
700/**************************************************************************
701* qla2xxx_eh_device_reset
702*
703* Description:
704* The device reset function will reset the target and abort any
705* executing commands.
706*
707* NOTE: The use of SP is undefined within this context. Do *NOT*
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700708* attempt to use this value, even if you determine it is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709* non-null.
710*
711* Input:
712* cmd = Linux SCSI command packet of the command that cause the
713* bus device reset.
714*
715* Returns:
716* SUCCESS/FAILURE (defined as macro in scsi.h).
717*
718**************************************************************************/
719int
720qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
721{
f4f051e2005-04-17 15:02:26 -0500722 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500723 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500724 srb_t *sp;
725 int ret;
726 unsigned int id, lun;
727 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
f4f051e2005-04-17 15:02:26 -0500729 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
f4f051e2005-04-17 15:02:26 -0500731 id = cmd->device->id;
732 lun = cmd->device->lun;
733 serial = cmd->serial_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
f4f051e2005-04-17 15:02:26 -0500735 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500736 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500737 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500740 "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -0400742 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 goto eh_dev_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500746 if (qla2x00_device_reset(ha, fcport) == 0)
747 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749#if defined(LOGOUT_AFTER_DEVICE_RESET)
f4f051e2005-04-17 15:02:26 -0500750 if (ret == SUCCESS) {
751 if (fcport->flags & FC_FABRIC_DEVICE) {
Andrew Vasquezabbd8872005-07-06 10:30:05 -0700752 ha->isp_ops.fabric_logout(ha, fcport->loop_id);
f4f051e2005-04-17 15:02:26 -0500753 qla2x00_mark_device_lost(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 }
756#endif
757 } else {
758 DEBUG2(printk(KERN_INFO
759 "%s failed: loop not ready\n",__func__);)
760 }
761
f4f051e2005-04-17 15:02:26 -0500762 if (ret == FAILED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 DEBUG3(printk("%s(%ld): device reset failed\n",
764 __func__, ha->host_no));
765 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
766 __func__);
767
768 goto eh_dev_reset_done;
769 }
770
Andrew Vasquez9a41a622005-09-20 13:25:53 -0700771 /* Flush outstanding commands. */
772 if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
773 ret = FAILED;
774 if (ret == FAILED) {
775 DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
776 __func__, ha->host_no));
777 qla_printk(KERN_INFO, ha,
778 "%s: failed while waiting for commands\n", __func__);
779 } else
780 qla_printk(KERN_INFO, ha,
781 "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
782 id, lun);
James Bottomley28f22b02005-10-28 17:22:18 -0500783 eh_dev_reset_done:
f4f051e2005-04-17 15:02:26 -0500784 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/**************************************************************************
788* qla2x00_eh_wait_for_pending_commands
789*
790* Description:
791* Waits for all the commands to come back from the specified host.
792*
793* Input:
794* ha - pointer to scsi_qla_host structure.
795*
796* Returns:
797* 1 : SUCCESS
798* 0 : FAILED
799*
800* Note:
801**************************************************************************/
802static int
803qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
804{
805 int cnt;
806 int status;
807 srb_t *sp;
808 struct scsi_cmnd *cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700809 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 status = 1;
812
813 /*
814 * Waiting for all commands for the designated target in the active
815 * array
816 */
817 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700818 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 sp = ha->outstanding_cmds[cnt];
820 if (sp) {
821 cmd = sp->cmd;
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700822 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 status = qla2x00_eh_wait_on_command(ha, cmd);
824 if (status == 0)
825 break;
826 }
827 else {
Andrew Vasquez18e144d2005-05-27 15:04:47 -0700828 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
831 return (status);
832}
833
834
835/**************************************************************************
836* qla2xxx_eh_bus_reset
837*
838* Description:
839* The bus reset function will reset the bus and abort any executing
840* commands.
841*
842* Input:
843* cmd = Linux SCSI command packet of the command that cause the
844* bus reset.
845*
846* Returns:
847* SUCCESS/FAILURE (defined as macro in scsi.h).
848*
849**************************************************************************/
850int
851qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
852{
f4f051e2005-04-17 15:02:26 -0500853 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500854 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 srb_t *sp;
f4f051e2005-04-17 15:02:26 -0500856 int ret;
857 unsigned int id, lun;
858 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
f4f051e2005-04-17 15:02:26 -0500860 ret = FAILED;
861
862 id = cmd->device->id;
863 lun = cmd->device->lun;
864 serial = cmd->serial_number;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500867 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500868 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500871 "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
874 DEBUG2(printk("%s failed:board disabled\n",__func__));
f4f051e2005-04-17 15:02:26 -0500875 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
878 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500879 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
880 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
f4f051e2005-04-17 15:02:26 -0500882 if (ret == FAILED)
883 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Andrew Vasquez9a41a622005-09-20 13:25:53 -0700885 /* Flush outstanding commands. */
886 if (!qla2x00_eh_wait_for_pending_commands(ha))
887 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
f4f051e2005-04-17 15:02:26 -0500889eh_bus_reset_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
f4f051e2005-04-17 15:02:26 -0500891 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
f4f051e2005-04-17 15:02:26 -0500893 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896/**************************************************************************
897* qla2xxx_eh_host_reset
898*
899* Description:
900* The reset function will reset the Adapter.
901*
902* Input:
903* cmd = Linux SCSI command packet of the command that cause the
904* adapter reset.
905*
906* Returns:
907* Either SUCCESS or FAILED.
908*
909* Note:
910**************************************************************************/
911int
912qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
913{
f4f051e2005-04-17 15:02:26 -0500914 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500915 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500916 srb_t *sp;
917 int ret;
918 unsigned int id, lun;
919 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
f4f051e2005-04-17 15:02:26 -0500921 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
f4f051e2005-04-17 15:02:26 -0500923 id = cmd->device->id;
924 lun = cmd->device->lun;
925 serial = cmd->serial_number;
926
927 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500928 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500929 return ret;
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500932 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500935 goto eh_host_reset_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 /*
938 * Fixme-may be dpc thread is active and processing
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700939 * loop_resync,so wait a while for it to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * be completed and then issue big hammer.Otherwise
941 * it may cause I/O failure as big hammer marks the
942 * devices as lost kicking of the port_down_timer
943 * while dpc is stuck for the mailbox to complete.
944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 qla2x00_wait_for_loop_ready(ha);
946 set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
947 if (qla2x00_abort_isp(ha)) {
948 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
949 /* failed. schedule dpc to try */
950 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
951
952 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500953 goto eh_host_reset_lock;
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /* Waiting for our command in done_queue to be returned to OS.*/
f4f051e2005-04-17 15:02:26 -0500958 if (qla2x00_eh_wait_for_pending_commands(ha))
959 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
f4f051e2005-04-17 15:02:26 -0500961eh_host_reset_lock:
f4f051e2005-04-17 15:02:26 -0500962 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
963 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
f4f051e2005-04-17 15:02:26 -0500965 return ret;
966}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968/*
969* qla2x00_loop_reset
970* Issue loop reset.
971*
972* Input:
973* ha = adapter block pointer.
974*
975* Returns:
976* 0 = success
977*/
978static int
979qla2x00_loop_reset(scsi_qla_host_t *ha)
980{
981 int status = QLA_SUCCESS;
bdf79622005-04-17 15:06:53 -0500982 struct fc_port *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 if (ha->flags.enable_lip_reset) {
985 status = qla2x00_lip_reset(ha);
986 }
987
988 if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
bdf79622005-04-17 15:06:53 -0500989 list_for_each_entry(fcport, &ha->fcports, list) {
990 if (fcport->port_type != FCT_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 continue;
992
Andrew Vasquezc00c72a2005-08-26 19:08:50 -0700993 status = qla2x00_device_reset(ha, fcport);
bdf79622005-04-17 15:06:53 -0500994 if (status != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 }
998
999 if (status == QLA_SUCCESS &&
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001000 ((!ha->flags.enable_target_reset &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 !ha->flags.enable_lip_reset) ||
1002 ha->flags.enable_lip_full_login)) {
1003
1004 status = qla2x00_full_login_lip(ha);
1005 }
1006
1007 /* Issue marker command only when we are going to start the I/O */
1008 ha->marker_needed = 1;
1009
1010 if (status) {
1011 /* Empty */
1012 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
1013 __func__,
1014 ha->host_no);)
1015 } else {
1016 /* Empty */
1017 DEBUG3(printk("%s(%ld): exiting normally.\n",
1018 __func__,
1019 ha->host_no);)
1020 }
1021
1022 return(status);
1023}
1024
1025/*
1026 * qla2x00_device_reset
1027 * Issue bus device reset message to the target.
1028 *
1029 * Input:
1030 * ha = adapter block pointer.
1031 * t = SCSI ID.
1032 * TARGET_QUEUE_LOCK must be released.
1033 * ADAPTER_STATE_LOCK must be released.
1034 *
1035 * Context:
1036 * Kernel context.
1037 */
1038static int
1039qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
1040{
1041 /* Abort Target command will clear Reservation */
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001042 return ha->isp_ops.abort_target(reset_fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
f4f051e2005-04-17 15:02:26 -05001045static int
1046qla2xxx_slave_alloc(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
bdf79622005-04-17 15:06:53 -05001048 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001050 if (!rport || fc_remote_port_chkready(rport))
f4f051e2005-04-17 15:02:26 -05001051 return -ENXIO;
1052
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001053 sdev->hostdata = *(fc_port_t **)rport->dd_data;
f4f051e2005-04-17 15:02:26 -05001054
1055 return 0;
1056}
1057
1058static int
1059qla2xxx_slave_configure(struct scsi_device *sdev)
1060{
8482e1182005-04-17 15:04:54 -05001061 scsi_qla_host_t *ha = to_qla_host(sdev->host);
1062 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1063
f4f051e2005-04-17 15:02:26 -05001064 if (sdev->tagged_supported)
1065 scsi_activate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 else
f4f051e2005-04-17 15:02:26 -05001067 scsi_deactivate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
8482e1182005-04-17 15:04:54 -05001069 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1070
f4f051e2005-04-17 15:02:26 -05001071 return 0;
1072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
f4f051e2005-04-17 15:02:26 -05001074static void
1075qla2xxx_slave_destroy(struct scsi_device *sdev)
1076{
1077 sdev->hostdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Andrew Vasquezce7e4af2005-08-26 19:09:30 -07001080static int
1081qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
1082{
1083 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1084 return sdev->queue_depth;
1085}
1086
1087static int
1088qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1089{
1090 if (sdev->tagged_supported) {
1091 scsi_set_tag_type(sdev, tag_type);
1092 if (tag_type)
1093 scsi_activate_tcq(sdev, sdev->queue_depth);
1094 else
1095 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1096 } else
1097 tag_type = 0;
1098
1099 return tag_type;
1100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/**
1103 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1104 * @ha: HA context
1105 *
1106 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1107 * supported addressing method.
1108 */
1109static void
1110qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1111{
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001112 /* Assume a 32bit DMA mask. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 ha->flags.enable_64bit_addressing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001115 if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
1116 /* Any upper-dword bits set? */
1117 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1118 !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1119 /* Ok, a 64bit DMA mask is applicable. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ha->flags.enable_64bit_addressing = 1;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001121 ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
1122 ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001123 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Andrew Vasquez7524f9b2005-08-26 19:08:00 -07001126
1127 dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
1128 pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
1131static int
1132qla2x00_iospace_config(scsi_qla_host_t *ha)
1133{
1134 unsigned long pio, pio_len, pio_flags;
1135 unsigned long mmio, mmio_len, mmio_flags;
1136
1137 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1138 pio = pci_resource_start(ha->pdev, 0);
1139 pio_len = pci_resource_len(ha->pdev, 0);
1140 pio_flags = pci_resource_flags(ha->pdev, 0);
1141 if (pio_flags & IORESOURCE_IO) {
1142 if (pio_len < MIN_IOBASE_LEN) {
1143 qla_printk(KERN_WARNING, ha,
1144 "Invalid PCI I/O region size (%s)...\n",
1145 pci_name(ha->pdev));
1146 pio = 0;
1147 }
1148 } else {
1149 qla_printk(KERN_WARNING, ha,
1150 "region #0 not a PIO resource (%s)...\n",
1151 pci_name(ha->pdev));
1152 pio = 0;
1153 }
1154
1155 /* Use MMIO operations for all accesses. */
1156 mmio = pci_resource_start(ha->pdev, 1);
1157 mmio_len = pci_resource_len(ha->pdev, 1);
1158 mmio_flags = pci_resource_flags(ha->pdev, 1);
1159
1160 if (!(mmio_flags & IORESOURCE_MEM)) {
1161 qla_printk(KERN_ERR, ha,
1162 "region #0 not an MMIO resource (%s), aborting\n",
1163 pci_name(ha->pdev));
1164 goto iospace_error_exit;
1165 }
1166 if (mmio_len < MIN_IOBASE_LEN) {
1167 qla_printk(KERN_ERR, ha,
1168 "Invalid PCI mem region size (%s), aborting\n",
1169 pci_name(ha->pdev));
1170 goto iospace_error_exit;
1171 }
1172
1173 if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1174 qla_printk(KERN_WARNING, ha,
1175 "Failed to reserve PIO/MMIO regions (%s)\n",
1176 pci_name(ha->pdev));
1177
1178 goto iospace_error_exit;
1179 }
1180
1181 ha->pio_address = pio;
1182 ha->pio_length = pio_len;
1183 ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1184 if (!ha->iobase) {
1185 qla_printk(KERN_ERR, ha,
1186 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1187
1188 goto iospace_error_exit;
1189 }
1190
1191 return (0);
1192
1193iospace_error_exit:
1194 return (-ENOMEM);
1195}
1196
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001197static void
1198qla2x00_enable_intrs(scsi_qla_host_t *ha)
1199{
1200 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001201 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001202
1203 spin_lock_irqsave(&ha->hardware_lock, flags);
1204 ha->interrupts_on = 1;
1205 /* enable risc and host interrupts */
1206 WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1207 RD_REG_WORD(&reg->ictrl);
1208 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1209
1210}
1211
1212static void
1213qla2x00_disable_intrs(scsi_qla_host_t *ha)
1214{
1215 unsigned long flags = 0;
Andrew Vasquez3d716442005-07-06 10:30:26 -07001216 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001217
1218 spin_lock_irqsave(&ha->hardware_lock, flags);
1219 ha->interrupts_on = 0;
1220 /* disable risc and host interrupts */
1221 WRT_REG_WORD(&reg->ictrl, 0);
1222 RD_REG_WORD(&reg->ictrl);
1223 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1224}
1225
Andrew Vasquezfca29702005-07-06 10:31:47 -07001226static void
1227qla24xx_enable_intrs(scsi_qla_host_t *ha)
1228{
1229 unsigned long flags = 0;
1230 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1231
1232 spin_lock_irqsave(&ha->hardware_lock, flags);
1233 ha->interrupts_on = 1;
1234 WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1235 RD_REG_DWORD(&reg->ictrl);
1236 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1237}
1238
1239static void
1240qla24xx_disable_intrs(scsi_qla_host_t *ha)
1241{
1242 unsigned long flags = 0;
1243 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1244
1245 spin_lock_irqsave(&ha->hardware_lock, flags);
1246 ha->interrupts_on = 0;
1247 WRT_REG_DWORD(&reg->ictrl, 0);
1248 RD_REG_DWORD(&reg->ictrl);
1249 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1250}
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252/*
1253 * PCI driver interface
1254 */
1255int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1256{
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001257 int ret = -ENODEV;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001258 device_reg_t __iomem *reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct Scsi_Host *host;
1260 scsi_qla_host_t *ha;
1261 unsigned long flags = 0;
1262 unsigned long wait_switch = 0;
1263 char pci_info[20];
1264 char fw_str[30];
8482e1182005-04-17 15:04:54 -05001265 fc_port_t *fcport;
Andrew Vasquez54333832005-11-09 15:49:04 -08001266 struct scsi_host_template *sht;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 if (pci_enable_device(pdev))
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001269 goto probe_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Andrew Vasquez54333832005-11-09 15:49:04 -08001271 sht = &qla2x00_driver_template;
1272 if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1273 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432)
1274 sht = &qla24xx_driver_template;
1275 host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (host == NULL) {
1277 printk(KERN_WARNING
1278 "qla2xxx: Couldn't allocate host from scsi layer!\n");
1279 goto probe_disable_device;
1280 }
1281
1282 /* Clear our data area */
1283 ha = (scsi_qla_host_t *)host->hostdata;
1284 memset(ha, 0, sizeof(scsi_qla_host_t));
1285
1286 ha->pdev = pdev;
1287 ha->host = host;
1288 ha->host_no = host->host_no;
1289 ha->brd_info = brd_info;
1290 sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1291
Andrew Morton444d1d92005-10-25 11:00:56 -07001292 ha->dpc_pid = -1;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /* Configure PCI I/O space */
1295 ret = qla2x00_iospace_config(ha);
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001296 if (ret)
1297 goto probe_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 qla_printk(KERN_INFO, ha,
Andrew Vasquez54333832005-11-09 15:49:04 -08001300 "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1301 ha->iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 spin_lock_init(&ha->hardware_lock);
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 ha->prev_topology = 0;
1306 ha->ports = MAX_BUSES;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001307 ha->init_cb_size = sizeof(init_cb_t);
Andrew Vasquezcca53352005-08-26 19:08:30 -07001308 ha->mgmt_svr_loop_id = MANAGEMENT_SERVER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001310 /* Assign ISP specific operations. */
1311 ha->isp_ops.pci_config = qla2100_pci_config;
1312 ha->isp_ops.reset_chip = qla2x00_reset_chip;
1313 ha->isp_ops.chip_diag = qla2x00_chip_diag;
1314 ha->isp_ops.config_rings = qla2x00_config_rings;
1315 ha->isp_ops.reset_adapter = qla2x00_reset_adapter;
1316 ha->isp_ops.nvram_config = qla2x00_nvram_config;
1317 ha->isp_ops.update_fw_options = qla2x00_update_fw_options;
Andrew Vasquez0107109e2005-07-06 10:31:37 -07001318 ha->isp_ops.load_risc = qla2x00_load_risc;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001319 ha->isp_ops.pci_info_str = qla2x00_pci_info_str;
1320 ha->isp_ops.fw_version_str = qla2x00_fw_version_str;
1321 ha->isp_ops.intr_handler = qla2100_intr_handler;
1322 ha->isp_ops.enable_intrs = qla2x00_enable_intrs;
1323 ha->isp_ops.disable_intrs = qla2x00_disable_intrs;
1324 ha->isp_ops.abort_command = qla2x00_abort_command;
1325 ha->isp_ops.abort_target = qla2x00_abort_target;
1326 ha->isp_ops.fabric_login = qla2x00_login_fabric;
1327 ha->isp_ops.fabric_logout = qla2x00_fabric_logout;
1328 ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_32;
1329 ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_32;
1330 ha->isp_ops.prep_ms_iocb = qla2x00_prep_ms_iocb;
Andrew Vasquezcca53352005-08-26 19:08:30 -07001331 ha->isp_ops.prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb;
Andrew Vasquez459c5372005-07-06 10:31:07 -07001332 ha->isp_ops.read_nvram = qla2x00_read_nvram_data;
1333 ha->isp_ops.write_nvram = qla2x00_write_nvram_data;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001334 ha->isp_ops.fw_dump = qla2100_fw_dump;
1335 ha->isp_ops.ascii_fw_dump = qla2100_ascii_fw_dump;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (IS_QLA2100(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001337 host->max_id = MAX_TARGETS_2100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1339 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1340 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1341 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1342 host->sg_tablesize = 32;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001343 ha->gid_list_info_size = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 } else if (IS_QLA2200(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001345 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1347 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1348 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1349 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001350 ha->gid_list_info_size = 4;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001351 } else if (IS_QLA23XX(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001352 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1354 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1355 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1356 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001357 ha->isp_ops.pci_config = qla2300_pci_config;
1358 ha->isp_ops.intr_handler = qla2300_intr_handler;
1359 ha->isp_ops.fw_dump = qla2300_fw_dump;
1360 ha->isp_ops.ascii_fw_dump = qla2300_ascii_fw_dump;
1361 ha->gid_list_info_size = 6;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001362 } else if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1363 host->max_id = MAX_TARGETS_2200;
1364 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1365 ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
1366 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1367 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1368 ha->init_cb_size = sizeof(struct init_cb_24xx);
Andrew Vasquezcca53352005-08-26 19:08:30 -07001369 ha->mgmt_svr_loop_id = 10;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001370 ha->isp_ops.pci_config = qla24xx_pci_config;
1371 ha->isp_ops.reset_chip = qla24xx_reset_chip;
1372 ha->isp_ops.chip_diag = qla24xx_chip_diag;
1373 ha->isp_ops.config_rings = qla24xx_config_rings;
1374 ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
1375 ha->isp_ops.nvram_config = qla24xx_nvram_config;
1376 ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
Andrew Vasquez54333832005-11-09 15:49:04 -08001377 ha->isp_ops.load_risc = qla24xx_load_risc;
Andrew Vasquez331e3472005-11-09 15:49:19 -08001378#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
1379 if (ql2xfwloadflash)
1380 ha->isp_ops.load_risc = qla24xx_load_risc_flash;
Andrew Vasquez54333832005-11-09 15:49:04 -08001381#endif
Andrew Vasquezfca29702005-07-06 10:31:47 -07001382 ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
1383 ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
1384 ha->isp_ops.intr_handler = qla24xx_intr_handler;
1385 ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
1386 ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
1387 ha->isp_ops.abort_command = qla24xx_abort_command;
1388 ha->isp_ops.abort_target = qla24xx_abort_target;
1389 ha->isp_ops.fabric_login = qla24xx_login_fabric;
1390 ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
1391 ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
Andrew Vasquezcca53352005-08-26 19:08:30 -07001392 ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
Andrew Vasquezfca29702005-07-06 10:31:47 -07001393 ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
1394 ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
1395 ha->isp_ops.fw_dump = qla24xx_fw_dump;
1396 ha->isp_ops.ascii_fw_dump = qla24xx_ascii_fw_dump;
1397 ha->gid_list_info_size = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 host->can_queue = ha->request_q_length + 128;
1400
1401 /* load the F/W, read paramaters, and init the H/W */
1402 ha->instance = num_hosts;
1403
1404 init_MUTEX(&ha->mbx_cmd_sem);
1405 init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1406
1407 INIT_LIST_HEAD(&ha->list);
1408 INIT_LIST_HEAD(&ha->fcports);
1409 INIT_LIST_HEAD(&ha->rscn_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /*
1412 * These locks are used to prevent more than one CPU
1413 * from modifying the queue at the same time. The
1414 * higher level "host_lock" will reduce most
1415 * contention for these locks.
1416 */
1417 spin_lock_init(&ha->mbx_reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 init_completion(&ha->dpc_inited);
1420 init_completion(&ha->dpc_exited);
1421
1422 qla2x00_config_dma_addressing(ha);
1423 if (qla2x00_mem_alloc(ha)) {
1424 qla_printk(KERN_WARNING, ha,
1425 "[ERROR] Failed to allocate memory for adapter\n");
1426
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001427 ret = -ENOMEM;
1428 goto probe_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430
1431 if (qla2x00_initialize_adapter(ha) &&
1432 !(ha->device_flags & DFLG_NO_CABLE)) {
1433
1434 qla_printk(KERN_WARNING, ha,
1435 "Failed to initialize adapter\n");
1436
1437 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1438 "Adapter flags %x.\n",
1439 ha->host_no, ha->device_flags));
1440
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001441 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 goto probe_failed;
1443 }
1444
1445 /*
1446 * Startup the kernel thread for this host adapter
1447 */
1448 ha->dpc_should_die = 0;
1449 ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1450 if (ha->dpc_pid < 0) {
1451 qla_printk(KERN_WARNING, ha,
1452 "Unable to start DPC thread!\n");
1453
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001454 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 goto probe_failed;
1456 }
1457 wait_for_completion(&ha->dpc_inited);
1458
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001459 host->this_id = 255;
1460 host->cmd_per_lun = 3;
1461 host->unique_id = ha->instance;
1462 host->max_cmd_len = MAX_CMDSZ;
1463 host->max_channel = ha->ports - 1;
1464 host->max_lun = MAX_LUNS;
1465 host->transportt = qla2xxx_transport_template;
1466
Andrew Vasquezfca29702005-07-06 10:31:47 -07001467 ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001468 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001469 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 qla_printk(KERN_WARNING, ha,
1471 "Failed to reserve interrupt %d already in use.\n",
Andrew Vasquezfca29702005-07-06 10:31:47 -07001472 pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 goto probe_failed;
1474 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07001475 host->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /* Initialized the timer */
1478 qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1479
1480 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1481 ha->host_no, ha));
1482
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001483 ha->isp_ops.disable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquezfca29702005-07-06 10:31:47 -07001486 reg = ha->iobase;
1487 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1488 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
1489 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
1490 } else {
1491 WRT_REG_WORD(&reg->isp.semaphore, 0);
1492 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
1493 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Andrew Vasquezfca29702005-07-06 10:31:47 -07001495 /* Enable proper parity */
1496 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1497 if (IS_QLA2300(ha))
1498 /* SRAM parity */
1499 WRT_REG_WORD(&reg->isp.hccr,
1500 (HCCR_ENABLE_PARITY + 0x1));
1501 else
1502 /* SRAM, Instruction RAM and GP RAM parity */
1503 WRT_REG_WORD(&reg->isp.hccr,
1504 (HCCR_ENABLE_PARITY + 0x7));
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1508
Andrew Vasquezabbd8872005-07-06 10:30:05 -07001509 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 /* v2.19.5b6 */
1512 /*
1513 * Wait around max loop_reset_delay secs for the devices to come
1514 * on-line. We don't want Linux scanning before we are ready.
1515 *
1516 */
1517 for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1518 time_before(jiffies,wait_switch) &&
1519 !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1520 && (ha->device_flags & SWITCH_FOUND) ;) {
1521
1522 qla2x00_check_fabric_devices(ha);
1523
1524 msleep(10);
1525 }
1526
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001527 pci_set_drvdata(pdev, ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ha->flags.init_done = 1;
1529 num_hosts++;
1530
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001531 ret = scsi_add_host(host, &pdev->dev);
1532 if (ret)
1533 goto probe_failed;
1534
1535 qla2x00_alloc_sysfs_attr(ha);
1536
1537 qla2x00_init_host_attr(ha);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 qla_printk(KERN_INFO, ha, "\n"
1540 " QLogic Fibre Channel HBA Driver: %s\n"
1541 " QLogic %s - %s\n"
Andrew Vasquez54333832005-11-09 15:49:04 -08001542 " ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
1543 qla2x00_version_str, ha->model_number,
1544 ha->model_desc ? ha->model_desc: "", pdev->device,
1545 ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
1546 ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
1547 ha->isp_ops.fw_version_str(ha, fw_str));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
8482e1182005-04-17 15:04:54 -05001549 /* Go with fc_rport registration. */
1550 list_for_each_entry(fcport, &ha->fcports, list)
1551 qla2x00_reg_remote_port(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 return 0;
1554
1555probe_failed:
1556 qla2x00_free_device(ha);
1557
1558 scsi_host_put(host);
1559
1560probe_disable_device:
1561 pci_disable_device(pdev);
1562
Andrew Vasqueza1541d52005-06-09 17:21:28 -07001563probe_out:
1564 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1567
1568void qla2x00_remove_one(struct pci_dev *pdev)
1569{
1570 scsi_qla_host_t *ha;
1571
1572 ha = pci_get_drvdata(pdev);
1573
8482e1182005-04-17 15:04:54 -05001574 qla2x00_free_sysfs_attr(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
bdf79622005-04-17 15:06:53 -05001576 fc_remove_host(ha->host);
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 scsi_remove_host(ha->host);
1579
1580 qla2x00_free_device(ha);
1581
1582 scsi_host_put(ha->host);
1583
1584 pci_set_drvdata(pdev, NULL);
1585}
1586EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1587
1588static void
1589qla2x00_free_device(scsi_qla_host_t *ha)
1590{
1591 int ret;
1592
1593 /* Abort any outstanding IO descriptors. */
1594 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1595 qla2x00_cancel_io_descriptors(ha);
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /* Disable timer */
1598 if (ha->timer_active)
1599 qla2x00_stop_timer(ha);
1600
1601 /* Kill the kernel thread for this host */
1602 if (ha->dpc_pid >= 0) {
1603 ha->dpc_should_die = 1;
1604 wmb();
1605 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1606 if (ret) {
1607 qla_printk(KERN_ERR, ha,
1608 "Unable to signal DPC thread -- (%d)\n", ret);
1609
1610 /* TODO: SOMETHING MORE??? */
1611 } else {
1612 wait_for_completion(&ha->dpc_exited);
1613 }
1614 }
1615
Andrew Vasquezf6ef3b12005-08-26 19:10:20 -07001616 /* Stop currently executing firmware. */
1617 qla2x00_stop_firmware(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Andrew Vasquezf6ef3b12005-08-26 19:10:20 -07001619 /* turn-off interrupts on the card */
1620 if (ha->interrupts_on)
1621 ha->isp_ops.disable_intrs(ha);
1622
1623 qla2x00_mem_free(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 ha->flags.online = 0;
1626
1627 /* Detach interrupts */
1628 if (ha->pdev->irq)
1629 free_irq(ha->pdev->irq, ha);
1630
1631 /* release io space registers */
1632 if (ha->iobase)
1633 iounmap(ha->iobase);
1634 pci_release_regions(ha->pdev);
1635
1636 pci_disable_device(ha->pdev);
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1641 *
1642 * Input: ha = adapter block pointer. fcport = port structure pointer.
1643 *
1644 * Return: None.
1645 *
1646 * Context:
1647 */
1648void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1649 int do_login)
1650{
8482e1182005-04-17 15:04:54 -05001651 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001652 schedule_work(&fcport->rport_del_work);
1653
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001654 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * We may need to retry the login, so don't change the state of the
1656 * port but do the retries.
1657 */
1658 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1659 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1660
1661 if (!do_login)
1662 return;
1663
1664 if (fcport->login_retry == 0) {
1665 fcport->login_retry = ha->login_retry_count;
1666 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1667
1668 DEBUG(printk("scsi(%ld): Port login retry: "
1669 "%02x%02x%02x%02x%02x%02x%02x%02x, "
1670 "id = 0x%04x retry cnt=%d\n",
1671 ha->host_no,
1672 fcport->port_name[0],
1673 fcport->port_name[1],
1674 fcport->port_name[2],
1675 fcport->port_name[3],
1676 fcport->port_name[4],
1677 fcport->port_name[5],
1678 fcport->port_name[6],
1679 fcport->port_name[7],
1680 fcport->loop_id,
1681 fcport->login_retry));
1682 }
1683}
1684
1685/*
1686 * qla2x00_mark_all_devices_lost
1687 * Updates fcport state when device goes offline.
1688 *
1689 * Input:
1690 * ha = adapter block pointer.
1691 * fcport = port structure pointer.
1692 *
1693 * Return:
1694 * None.
1695 *
1696 * Context:
1697 */
1698void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07001699qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 fc_port_t *fcport;
1702
1703 list_for_each_entry(fcport, &ha->fcports, list) {
1704 if (fcport->port_type != FCT_TARGET)
1705 continue;
1706
1707 /*
1708 * No point in marking the device as lost, if the device is
1709 * already DEAD.
1710 */
1711 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1712 continue;
8482e1182005-04-17 15:04:54 -05001713 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001714 schedule_work(&fcport->rport_del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1716 }
1717}
1718
1719/*
1720* qla2x00_mem_alloc
1721* Allocates adapter memory.
1722*
1723* Returns:
1724* 0 = success.
1725* 1 = failure.
1726*/
1727static uint8_t
1728qla2x00_mem_alloc(scsi_qla_host_t *ha)
1729{
1730 char name[16];
1731 uint8_t status = 1;
1732 int retry= 10;
1733
1734 do {
1735 /*
1736 * This will loop only once if everything goes well, else some
1737 * number of retries will be performed to get around a kernel
1738 * bug where available mem is not allocated until after a
1739 * little delay and a retry.
1740 */
1741 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1742 (ha->request_q_length + 1) * sizeof(request_t),
1743 &ha->request_dma, GFP_KERNEL);
1744 if (ha->request_ring == NULL) {
1745 qla_printk(KERN_WARNING, ha,
1746 "Memory Allocation failed - request_ring\n");
1747
1748 qla2x00_mem_free(ha);
1749 msleep(100);
1750
1751 continue;
1752 }
1753
1754 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1755 (ha->response_q_length + 1) * sizeof(response_t),
1756 &ha->response_dma, GFP_KERNEL);
1757 if (ha->response_ring == NULL) {
1758 qla_printk(KERN_WARNING, ha,
1759 "Memory Allocation failed - response_ring\n");
1760
1761 qla2x00_mem_free(ha);
1762 msleep(100);
1763
1764 continue;
1765 }
1766
1767 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1768 &ha->gid_list_dma, GFP_KERNEL);
1769 if (ha->gid_list == NULL) {
1770 qla_printk(KERN_WARNING, ha,
1771 "Memory Allocation failed - gid_list\n");
1772
1773 qla2x00_mem_free(ha);
1774 msleep(100);
1775
1776 continue;
1777 }
1778
1779 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1780 sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1781 if (ha->rlc_rsp == NULL) {
1782 qla_printk(KERN_WARNING, ha,
1783 "Memory Allocation failed - rlc");
1784
1785 qla2x00_mem_free(ha);
1786 msleep(100);
1787
1788 continue;
1789 }
1790
1791 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1792 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1793 DMA_POOL_SIZE, 8, 0);
1794 if (ha->s_dma_pool == NULL) {
1795 qla_printk(KERN_WARNING, ha,
1796 "Memory Allocation failed - s_dma_pool\n");
1797
1798 qla2x00_mem_free(ha);
1799 msleep(100);
1800
1801 continue;
1802 }
1803
1804 /* get consistent memory allocated for init control block */
1805 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1806 &ha->init_cb_dma);
1807 if (ha->init_cb == NULL) {
1808 qla_printk(KERN_WARNING, ha,
1809 "Memory Allocation failed - init_cb\n");
1810
1811 qla2x00_mem_free(ha);
1812 msleep(100);
1813
1814 continue;
1815 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07001816 memset(ha->init_cb, 0, ha->init_cb_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 /* Get consistent memory allocated for Get Port Database cmd */
1819 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1820 &ha->iodesc_pd_dma);
1821 if (ha->iodesc_pd == NULL) {
1822 /* error */
1823 qla_printk(KERN_WARNING, ha,
1824 "Memory Allocation failed - iodesc_pd\n");
1825
1826 qla2x00_mem_free(ha);
1827 msleep(100);
1828
1829 continue;
1830 }
1831 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1832
1833 /* Allocate ioctl related memory. */
1834 if (qla2x00_alloc_ioctl_mem(ha)) {
1835 qla_printk(KERN_WARNING, ha,
1836 "Memory Allocation failed - ioctl_mem\n");
1837
1838 qla2x00_mem_free(ha);
1839 msleep(100);
1840
1841 continue;
1842 }
1843
1844 if (qla2x00_allocate_sp_pool(ha)) {
1845 qla_printk(KERN_WARNING, ha,
1846 "Memory Allocation failed - "
1847 "qla2x00_allocate_sp_pool()\n");
1848
1849 qla2x00_mem_free(ha);
1850 msleep(100);
1851
1852 continue;
1853 }
1854
1855 /* Allocate memory for SNS commands */
1856 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1857 /* Get consistent memory allocated for SNS commands */
1858 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1859 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1860 GFP_KERNEL);
1861 if (ha->sns_cmd == NULL) {
1862 /* error */
1863 qla_printk(KERN_WARNING, ha,
1864 "Memory Allocation failed - sns_cmd\n");
1865
1866 qla2x00_mem_free(ha);
1867 msleep(100);
1868
1869 continue;
1870 }
1871 memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1872 } else {
1873 /* Get consistent memory allocated for MS IOCB */
1874 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1875 &ha->ms_iocb_dma);
1876 if (ha->ms_iocb == NULL) {
1877 /* error */
1878 qla_printk(KERN_WARNING, ha,
1879 "Memory Allocation failed - ms_iocb\n");
1880
1881 qla2x00_mem_free(ha);
1882 msleep(100);
1883
1884 continue;
1885 }
1886 memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1887
1888 /*
1889 * Get consistent memory allocated for CT SNS
1890 * commands
1891 */
1892 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1893 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1894 GFP_KERNEL);
1895 if (ha->ct_sns == NULL) {
1896 /* error */
1897 qla_printk(KERN_WARNING, ha,
1898 "Memory Allocation failed - ct_sns\n");
1899
1900 qla2x00_mem_free(ha);
1901 msleep(100);
1902
1903 continue;
1904 }
1905 memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1906 }
1907
1908 /* Done all allocations without any error. */
1909 status = 0;
1910
1911 } while (retry-- && status != 0);
1912
1913 if (status) {
1914 printk(KERN_WARNING
1915 "%s(): **** FAILED ****\n", __func__);
1916 }
1917
1918 return(status);
1919}
1920
1921/*
1922* qla2x00_mem_free
1923* Frees all adapter allocated memory.
1924*
1925* Input:
1926* ha = adapter block pointer.
1927*/
1928static void
1929qla2x00_mem_free(scsi_qla_host_t *ha)
1930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct list_head *fcpl, *fcptemp;
1932 fc_port_t *fcport;
Andrew Vasquezfe74c712005-08-26 19:10:10 -07001933 unsigned int wtime;/* max wait time if mbx cmd is busy. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 if (ha == NULL) {
1936 /* error */
1937 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1938 return;
1939 }
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* Make sure all other threads are stopped. */
Andrew Vasquezfe74c712005-08-26 19:10:10 -07001942 wtime = 60 * 1000;
1943 while (ha->dpc_wait && wtime)
1944 wtime = msleep_interruptible(wtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /* free ioctl memory */
1947 qla2x00_free_ioctl_mem(ha);
1948
1949 /* free sp pool */
1950 qla2x00_free_sp_pool(ha);
1951
1952 if (ha->sns_cmd)
1953 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1954 ha->sns_cmd, ha->sns_cmd_dma);
1955
1956 if (ha->ct_sns)
1957 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1958 ha->ct_sns, ha->ct_sns_dma);
1959
1960 if (ha->ms_iocb)
1961 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1962
1963 if (ha->iodesc_pd)
1964 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1965
1966 if (ha->init_cb)
1967 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1968
1969 if (ha->s_dma_pool)
1970 dma_pool_destroy(ha->s_dma_pool);
1971
1972 if (ha->rlc_rsp)
1973 dma_free_coherent(&ha->pdev->dev,
1974 sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1975 ha->rlc_rsp_dma);
1976
1977 if (ha->gid_list)
1978 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1979 ha->gid_list_dma);
1980
1981 if (ha->response_ring)
1982 dma_free_coherent(&ha->pdev->dev,
1983 (ha->response_q_length + 1) * sizeof(response_t),
1984 ha->response_ring, ha->response_dma);
1985
1986 if (ha->request_ring)
1987 dma_free_coherent(&ha->pdev->dev,
1988 (ha->request_q_length + 1) * sizeof(request_t),
1989 ha->request_ring, ha->request_dma);
1990
1991 ha->sns_cmd = NULL;
1992 ha->sns_cmd_dma = 0;
1993 ha->ct_sns = NULL;
1994 ha->ct_sns_dma = 0;
1995 ha->ms_iocb = NULL;
1996 ha->ms_iocb_dma = 0;
1997 ha->iodesc_pd = NULL;
1998 ha->iodesc_pd_dma = 0;
1999 ha->init_cb = NULL;
2000 ha->init_cb_dma = 0;
2001
2002 ha->s_dma_pool = NULL;
2003
2004 ha->rlc_rsp = NULL;
2005 ha->rlc_rsp_dma = 0;
2006 ha->gid_list = NULL;
2007 ha->gid_list_dma = 0;
2008
2009 ha->response_ring = NULL;
2010 ha->response_dma = 0;
2011 ha->request_ring = NULL;
2012 ha->request_dma = 0;
2013
2014 list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
2015 fcport = list_entry(fcpl, fc_port_t, list);
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /* fc ports */
2018 list_del_init(&fcport->list);
2019 kfree(fcport);
2020 }
2021 INIT_LIST_HEAD(&ha->fcports);
2022
2023 if (ha->fw_dump)
2024 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
2025
Andrew Vasquezfca29702005-07-06 10:31:47 -07002026 vfree(ha->fw_dump24);
2027
2028 vfree(ha->fw_dump_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 ha->fw_dump = NULL;
Andrew Vasquezfca29702005-07-06 10:31:47 -07002031 ha->fw_dump24 = NULL;
2032 ha->fw_dumped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 ha->fw_dump_reading = 0;
2034 ha->fw_dump_buffer = NULL;
2035}
2036
2037/*
2038 * qla2x00_allocate_sp_pool
2039 * This routine is called during initialization to allocate
2040 * memory for local srb_t.
2041 *
2042 * Input:
2043 * ha = adapter block pointer.
2044 *
2045 * Context:
2046 * Kernel context.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002047 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * Note: Sets the ref_count for non Null sp to one.
2049 */
2050static int
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002051qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 int rval;
2054
2055 rval = QLA_SUCCESS;
2056 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2057 mempool_free_slab, srb_cachep);
2058 if (ha->srb_mempool == NULL) {
2059 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
2060 rval = QLA_FUNCTION_FAILED;
2061 }
2062 return (rval);
2063}
2064
2065/*
2066 * This routine frees all adapter allocated memory.
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002067 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 */
2069static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002070qla2x00_free_sp_pool( scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 if (ha->srb_mempool) {
2073 mempool_destroy(ha->srb_mempool);
2074 ha->srb_mempool = NULL;
2075 }
2076}
2077
2078/**************************************************************************
2079* qla2x00_do_dpc
2080* This kernel thread is a task that is schedule by the interrupt handler
2081* to perform the background processing for interrupts.
2082*
2083* Notes:
2084* This task always run in the context of a kernel thread. It
2085* is kick-off by the driver's detect code and starts up
2086* up one per adapter. It immediately goes to sleep and waits for
2087* some fibre event. When either the interrupt handler or
2088* the timer routine detects a event it will one of the task
2089* bits then wake us up.
2090**************************************************************************/
2091static int
2092qla2x00_do_dpc(void *data)
2093{
2094 DECLARE_MUTEX_LOCKED(sem);
2095 scsi_qla_host_t *ha;
2096 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 uint8_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 uint16_t next_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 ha = (scsi_qla_host_t *)data;
2101
2102 lock_kernel();
2103
2104 daemonize("%s_dpc", ha->host_str);
2105 allow_signal(SIGHUP);
2106
2107 ha->dpc_wait = &sem;
2108
2109 set_user_nice(current, -20);
2110
2111 unlock_kernel();
2112
2113 complete(&ha->dpc_inited);
2114
2115 while (1) {
2116 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2117
2118 if (down_interruptible(&sem))
2119 break;
2120
2121 if (ha->dpc_should_die)
2122 break;
2123
2124 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2125
2126 /* Initialization not yet finished. Don't do anything yet. */
2127 if (!ha->flags.init_done || ha->dpc_active)
2128 continue;
2129
2130 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
2131
2132 ha->dpc_active = 1;
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (ha->flags.mbox_busy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 ha->dpc_active = 0;
2136 continue;
2137 }
2138
2139 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2140
2141 DEBUG(printk("scsi(%ld): dpc: sched "
2142 "qla2x00_abort_isp ha = %p\n",
2143 ha->host_no, ha));
2144 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2145 &ha->dpc_flags))) {
2146
2147 if (qla2x00_abort_isp(ha)) {
2148 /* failed. retry later */
2149 set_bit(ISP_ABORT_NEEDED,
2150 &ha->dpc_flags);
2151 }
2152 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
2153 }
2154 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2155 ha->host_no));
2156 }
2157
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002158 if (test_and_clear_bit(LOOP_RESET_NEEDED, &ha->dpc_flags)) {
2159 DEBUG(printk("scsi(%ld): dpc: sched loop_reset()\n",
2160 ha->host_no));
2161 qla2x00_loop_reset(ha);
2162 }
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
2165 (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
2166
2167 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2168 ha->host_no));
2169
2170 qla2x00_rst_aen(ha);
2171 clear_bit(RESET_ACTIVE, &ha->dpc_flags);
2172 }
2173
2174 /* Retry each device up to login retry count */
2175 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2176 !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
2177 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2178
2179 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2180 ha->host_no));
2181
2182 next_loopid = 0;
2183 list_for_each_entry(fcport, &ha->fcports, list) {
2184 if (fcport->port_type != FCT_TARGET)
2185 continue;
2186
2187 /*
2188 * If the port is not ONLINE then try to login
2189 * to it if we haven't run out of retries.
2190 */
2191 if (atomic_read(&fcport->state) != FCS_ONLINE &&
2192 fcport->login_retry) {
2193
2194 fcport->login_retry--;
2195 if (fcport->flags & FCF_FABRIC_DEVICE) {
2196 if (fcport->flags &
2197 FCF_TAPE_PRESENT)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002198 ha->isp_ops.fabric_logout(
Andrew Vasquez1c7c6352005-07-06 10:30:57 -07002199 ha, fcport->loop_id,
2200 fcport->d_id.b.domain,
2201 fcport->d_id.b.area,
2202 fcport->d_id.b.al_pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 status = qla2x00_fabric_login(
2204 ha, fcport, &next_loopid);
2205 } else
2206 status =
2207 qla2x00_local_device_login(
2208 ha, fcport->loop_id);
2209
2210 if (status == QLA_SUCCESS) {
2211 fcport->old_loop_id = fcport->loop_id;
2212
2213 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
2214 ha->host_no, fcport->loop_id));
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 fcport->port_login_retry_count =
2217 ha->port_down_retry_count * PORT_RETRY_TIME;
2218 atomic_set(&fcport->state, FCS_ONLINE);
2219 atomic_set(&fcport->port_down_timer,
2220 ha->port_down_retry_count * PORT_RETRY_TIME);
2221
2222 fcport->login_retry = 0;
2223 } else if (status == 1) {
2224 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
2225 /* retry the login again */
2226 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
2227 ha->host_no,
2228 fcport->login_retry, fcport->loop_id));
2229 } else {
2230 fcport->login_retry = 0;
2231 }
2232 }
2233 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2234 break;
2235 }
2236 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2237 ha->host_no));
2238 }
2239
2240 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2241 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2242
2243 clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2244 DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2245 ha->host_no));
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2248
2249 DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2250 ha->host_no));
2251 }
2252
2253 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2254
2255 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2256 ha->host_no));
2257
2258 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2259 &ha->dpc_flags))) {
2260
2261 qla2x00_loop_resync(ha);
2262
2263 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2264 }
2265
2266 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2267 ha->host_no));
2268 }
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2271
2272 DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2273 ha->host_no));
2274
2275 qla2x00_rescan_fcports(ha);
2276
2277 DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2278 "end.\n",
2279 ha->host_no));
2280 }
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (!ha->interrupts_on)
Andrew Vasquezabbd8872005-07-06 10:30:05 -07002283 ha->isp_ops.enable_intrs(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 ha->dpc_active = 0;
2286 } /* End of while(1) */
2287
2288 DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2289
2290 /*
2291 * Make sure that nobody tries to wake us up again.
2292 */
2293 ha->dpc_wait = NULL;
2294 ha->dpc_active = 0;
2295
2296 complete_and_exit(&ha->dpc_exited, 0);
2297}
2298
2299/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300* qla2x00_rst_aen
2301* Processes asynchronous reset.
2302*
2303* Input:
2304* ha = adapter block pointer.
2305*/
2306static void
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002307qla2x00_rst_aen(scsi_qla_host_t *ha)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 if (ha->flags.online && !ha->flags.reset_active &&
2310 !atomic_read(&ha->loop_down_timer) &&
2311 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2312 do {
2313 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2314
2315 /*
2316 * Issue marker command only when we are going to start
2317 * the I/O.
2318 */
2319 ha->marker_needed = 1;
2320 } while (!atomic_read(&ha->loop_down_timer) &&
2321 (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2322 }
2323}
2324
f4f051e2005-04-17 15:02:26 -05002325static void
2326qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2327{
2328 struct scsi_cmnd *cmd = sp->cmd;
2329
2330 if (sp->flags & SRB_DMA_VALID) {
2331 if (cmd->use_sg) {
2332 dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2333 cmd->use_sg, cmd->sc_data_direction);
2334 } else if (cmd->request_bufflen) {
2335 dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2336 cmd->request_bufflen, cmd->sc_data_direction);
2337 }
2338 sp->flags &= ~SRB_DMA_VALID;
2339 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07002340 CMD_SP(cmd) = NULL;
f4f051e2005-04-17 15:02:26 -05002341}
2342
2343void
2344qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2345{
2346 struct scsi_cmnd *cmd = sp->cmd;
2347
2348 qla2x00_sp_free_dma(ha, sp);
2349
f4f051e2005-04-17 15:02:26 -05002350 mempool_free(sp, ha->srb_mempool);
2351
2352 cmd->scsi_done(cmd);
2353}
bdf79622005-04-17 15:06:53 -05002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355/**************************************************************************
2356* qla2x00_timer
2357*
2358* Description:
2359* One second timer
2360*
2361* Context: Interrupt
2362***************************************************************************/
2363static void
2364qla2x00_timer(scsi_qla_host_t *ha)
2365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 unsigned long cpu_flags = 0;
2367 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 int start_dpc = 0;
2369 int index;
2370 srb_t *sp;
f4f051e2005-04-17 15:02:26 -05002371 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /*
2374 * Ports - Port down timer.
2375 *
2376 * Whenever, a port is in the LOST state we start decrementing its port
2377 * down timer every second until it reaches zero. Once it reaches zero
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002378 * the port it marked DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 */
2380 t = 0;
2381 list_for_each_entry(fcport, &ha->fcports, list) {
2382 if (fcport->port_type != FCT_TARGET)
2383 continue;
2384
2385 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2386
2387 if (atomic_read(&fcport->port_down_timer) == 0)
2388 continue;
2389
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002390 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
Andrew Vasquezfca29702005-07-06 10:31:47 -07002394 "%d remaining\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 ha->host_no,
2396 t, atomic_read(&fcport->port_down_timer)));
2397 }
2398 t++;
2399 } /* End of for fcport */
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 /* Loop down handler. */
2403 if (atomic_read(&ha->loop_down_timer) > 0 &&
2404 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2405
2406 if (atomic_read(&ha->loop_down_timer) ==
2407 ha->loop_down_abort_time) {
2408
2409 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2410 "queues before time expire\n",
2411 ha->host_no));
2412
2413 if (!IS_QLA2100(ha) && ha->link_down_timeout)
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -07002414 atomic_set(&ha->loop_state, LOOP_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 /* Schedule an ISP abort to return any tape commands. */
2417 spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2418 for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2419 index++) {
bdf79622005-04-17 15:06:53 -05002420 fc_port_t *sfcp;
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 sp = ha->outstanding_cmds[index];
2423 if (!sp)
2424 continue;
bdf79622005-04-17 15:06:53 -05002425 sfcp = sp->fcport;
2426 if (!(sfcp->flags & FCF_TAPE_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 continue;
2428
2429 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2430 break;
2431 }
2432 spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2433
2434 set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2435 start_dpc++;
2436 }
2437
2438 /* if the loop has been down for 4 minutes, reinit adapter */
2439 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2440 DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2441 "restarting queues.\n",
2442 ha->host_no));
2443
2444 set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2445 start_dpc++;
2446
2447 if (!(ha->device_flags & DFLG_NO_CABLE)) {
2448 DEBUG(printk("scsi(%ld): Loop down - "
2449 "aborting ISP.\n",
2450 ha->host_no));
2451 qla_printk(KERN_WARNING, ha,
2452 "Loop down - aborting ISP.\n");
2453
2454 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2455 }
2456 }
Andrew Vasquezfca29702005-07-06 10:31:47 -07002457 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 ha->host_no,
2459 atomic_read(&ha->loop_down_timer)));
2460 }
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /* Schedule the DPC routine if needed */
2463 if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2464 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
Andrew Vasquez91ca7b02005-10-27 16:03:37 -07002465 test_bit(LOOP_RESET_NEEDED, &ha->dpc_flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 start_dpc ||
2467 test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
f4f051e2005-04-17 15:02:26 -05002468 test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2470 ha->dpc_wait && !ha->dpc_active) {
2471
2472 up(ha->dpc_wait);
2473 }
2474
2475 qla2x00_restart_timer(ha, WATCH_INTERVAL);
2476}
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478/* XXX(hch): crude hack to emulate a down_timeout() */
2479int
2480qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2481{
Andrew Vasquezfe74c712005-08-26 19:10:10 -07002482 const unsigned int step = 100; /* msecs */
2483 unsigned int iterations = jiffies_to_msecs(timeout)/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 do {
2486 if (!down_trylock(sema))
2487 return 0;
Andrew Vasquezfe74c712005-08-26 19:10:10 -07002488 if (msleep_interruptible(step))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 break;
Andrew Vasquezfe74c712005-08-26 19:10:10 -07002490 } while (--iterations >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 return -ETIMEDOUT;
2493}
2494
Andrew Vasquez54333832005-11-09 15:49:04 -08002495#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
2496
2497#define qla2x00_release_firmware() do { } while (0)
Andrew Vasquez331e3472005-11-09 15:49:19 -08002498#define qla2x00_pci_module_init() (0)
2499#define qla2x00_pci_module_exit() do { } while (0)
Andrew Vasquezfca29702005-07-06 10:31:47 -07002500
Andrew Vasquez54333832005-11-09 15:49:04 -08002501#else /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
2502
2503/* Firmware interface routines. */
2504
2505#define FW_BLOBS 6
2506#define FW_ISP21XX 0
2507#define FW_ISP22XX 1
2508#define FW_ISP2300 2
2509#define FW_ISP2322 3
2510#define FW_ISP63XX 4
2511#define FW_ISP24XX 5
2512
2513static DECLARE_MUTEX(qla_fw_lock);
2514
2515static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
2516 { .name = "ql2100_fw.bin", .segs = { 0x1000, 0 }, },
2517 { .name = "ql2200_fw.bin", .segs = { 0x1000, 0 }, },
2518 { .name = "ql2300_fw.bin", .segs = { 0x800, 0 }, },
2519 { .name = "ql2322_fw.bin", .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
2520 { .name = "ql6312_fw.bin", .segs = { 0x800, 0 }, },
2521 { .name = "ql2400_fw.bin", },
2522};
2523
2524struct fw_blob *
2525qla2x00_request_firmware(scsi_qla_host_t *ha)
2526{
2527 struct fw_blob *blob;
2528
2529 blob = NULL;
2530 if (IS_QLA2100(ha)) {
2531 blob = &qla_fw_blobs[FW_ISP21XX];
2532 } else if (IS_QLA2200(ha)) {
2533 blob = &qla_fw_blobs[FW_ISP22XX];
2534 } else if (IS_QLA2300(ha) || IS_QLA2312(ha)) {
2535 blob = &qla_fw_blobs[FW_ISP2300];
2536 } else if (IS_QLA2322(ha)) {
2537 blob = &qla_fw_blobs[FW_ISP2322];
2538 } else if (IS_QLA6312(ha) || IS_QLA6322(ha)) {
2539 blob = &qla_fw_blobs[FW_ISP63XX];
2540 } else if (IS_QLA24XX(ha)) {
2541 blob = &qla_fw_blobs[FW_ISP24XX];
2542 }
2543
2544 down(&qla_fw_lock);
2545 if (blob->fw)
2546 goto out;
2547
2548 if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
2549 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
2550 "(%s).\n", ha->host_no, blob->name));
2551 blob->fw = NULL;
2552 blob = NULL;
2553 goto out;
2554 }
2555
2556out:
2557 up(&qla_fw_lock);
2558 return blob;
2559}
2560
2561static void
2562qla2x00_release_firmware(void)
2563{
2564 int idx;
2565
2566 down(&qla_fw_lock);
2567 for (idx = 0; idx < FW_BLOBS; idx++)
2568 if (qla_fw_blobs[idx].fw)
2569 release_firmware(qla_fw_blobs[idx].fw);
2570 up(&qla_fw_lock);
2571}
2572
2573static struct qla_board_info qla_board_tbl = {
2574 .drv_name = "qla2xxx",
2575};
2576
2577static struct pci_device_id qla2xxx_pci_tbl[] = {
2578 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100,
2579 PCI_ANY_ID, PCI_ANY_ID, },
2580 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200,
2581 PCI_ANY_ID, PCI_ANY_ID, },
2582 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300,
2583 PCI_ANY_ID, PCI_ANY_ID, },
2584 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312,
2585 PCI_ANY_ID, PCI_ANY_ID, },
2586 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322,
2587 PCI_ANY_ID, PCI_ANY_ID, },
2588 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312,
2589 PCI_ANY_ID, PCI_ANY_ID, },
2590 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322,
2591 PCI_ANY_ID, PCI_ANY_ID, },
2592 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422,
2593 PCI_ANY_ID, PCI_ANY_ID, },
2594 { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432,
2595 PCI_ANY_ID, PCI_ANY_ID, },
2596 { 0 },
2597};
2598MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
2599
2600static int __devinit
2601qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2602{
2603 return qla2x00_probe_one(pdev, &qla_board_tbl);
2604}
2605
2606static void __devexit
2607qla2xxx_remove_one(struct pci_dev *pdev)
2608{
2609 qla2x00_remove_one(pdev);
2610}
2611
Andrew Vasquezfca29702005-07-06 10:31:47 -07002612static struct pci_driver qla2xxx_pci_driver = {
2613 .name = "qla2xxx",
James Bottomley0a21ef12005-12-01 12:51:50 -06002614 .driver = {
2615 .owner = THIS_MODULE,
2616 },
Andrew Vasquezfca29702005-07-06 10:31:47 -07002617 .id_table = qla2xxx_pci_tbl,
2618 .probe = qla2xxx_probe_one,
2619 .remove = __devexit_p(qla2xxx_remove_one),
2620};
2621
Andrew Vasquez331e3472005-11-09 15:49:19 -08002622static inline int
2623qla2x00_pci_module_init(void)
2624{
2625 return pci_module_init(&qla2xxx_pci_driver);
2626}
2627
2628static inline void
2629qla2x00_pci_module_exit(void)
2630{
2631 pci_unregister_driver(&qla2xxx_pci_driver);
2632}
2633
2634#endif
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636/**
2637 * qla2x00_module_init - Module initialization.
2638 **/
2639static int __init
2640qla2x00_module_init(void)
2641{
Andrew Vasquezfca29702005-07-06 10:31:47 -07002642 int ret = 0;
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 /* Allocate cache for SRBs. */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002645 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 SLAB_HWCACHE_ALIGN, NULL, NULL);
2647 if (srb_cachep == NULL) {
2648 printk(KERN_ERR
2649 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2650 return -ENOMEM;
2651 }
2652
2653 /* Derive version string. */
2654 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
Andrew Vasquez54333832005-11-09 15:49:04 -08002655#if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
2656 strcat(qla2x00_version_str, "-fw");
2657#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658#if DEBUG_QLA2100
2659 strcat(qla2x00_version_str, "-debug");
2660#endif
Andrew Vasquez1c97a122005-04-21 16:13:36 -04002661 qla2xxx_transport_template =
2662 fc_attach_transport(&qla2xxx_transport_functions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (!qla2xxx_transport_template)
2664 return -ENODEV;
2665
2666 printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
Andrew Vasquez331e3472005-11-09 15:49:19 -08002667 ret = qla2x00_pci_module_init();
Andrew Vasquezfca29702005-07-06 10:31:47 -07002668 if (ret) {
2669 kmem_cache_destroy(srb_cachep);
2670 fc_release_transport(qla2xxx_transport_template);
2671 }
2672 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
2675/**
2676 * qla2x00_module_exit - Module cleanup.
2677 **/
2678static void __exit
2679qla2x00_module_exit(void)
2680{
Andrew Vasquez331e3472005-11-09 15:49:19 -08002681 qla2x00_pci_module_exit();
Andrew Vasquez54333832005-11-09 15:49:04 -08002682 qla2x00_release_firmware();
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002683 kmem_cache_destroy(srb_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 fc_release_transport(qla2xxx_transport_template);
2685}
2686
2687module_init(qla2x00_module_init);
2688module_exit(qla2x00_module_exit);
2689
2690MODULE_AUTHOR("QLogic Corporation");
2691MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2692MODULE_LICENSE("GPL");
2693MODULE_VERSION(QLA2XXX_VERSION);