]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/scsi/qla2xxx/qla_os.c
scsi: qla2xxx: Fix a race condition between aborting and completing a SCSI command
[mirror_ubuntu-hirsute-kernel.git] / drivers / scsi / qla2xxx / qla_os.c
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2014 QLogic Corporation
4 *
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
8
9 #include <linux/moduleparam.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
12 #include <linux/kthread.h>
13 #include <linux/mutex.h>
14 #include <linux/kobject.h>
15 #include <linux/slab.h>
16 #include <linux/blk-mq-pci.h>
17 #include <linux/refcount.h>
18
19 #include <scsi/scsi_tcq.h>
20 #include <scsi/scsicam.h>
21 #include <scsi/scsi_transport.h>
22 #include <scsi/scsi_transport_fc.h>
23
24 #include "qla_target.h"
25
26 /*
27 * Driver version
28 */
29 char qla2x00_version_str[40];
30
31 static int apidev_major;
32
33 /*
34 * SRB allocation cache
35 */
36 struct kmem_cache *srb_cachep;
37
38 /*
39 * CT6 CTX allocation cache
40 */
41 static struct kmem_cache *ctx_cachep;
42 /*
43 * error level for logging
44 */
45 uint ql_errlev = 0x8001;
46
47 static int ql2xenableclass2;
48 module_param(ql2xenableclass2, int, S_IRUGO|S_IRUSR);
49 MODULE_PARM_DESC(ql2xenableclass2,
50 "Specify if Class 2 operations are supported from the very "
51 "beginning. Default is 0 - class 2 not supported.");
52
53
54 int ql2xlogintimeout = 20;
55 module_param(ql2xlogintimeout, int, S_IRUGO);
56 MODULE_PARM_DESC(ql2xlogintimeout,
57 "Login timeout value in seconds.");
58
59 int qlport_down_retry;
60 module_param(qlport_down_retry, int, S_IRUGO);
61 MODULE_PARM_DESC(qlport_down_retry,
62 "Maximum number of command retries to a port that returns "
63 "a PORT-DOWN status.");
64
65 int ql2xplogiabsentdevice;
66 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
67 MODULE_PARM_DESC(ql2xplogiabsentdevice,
68 "Option to enable PLOGI to devices that are not present after "
69 "a Fabric scan. This is needed for several broken switches. "
70 "Default is 0 - no PLOGI. 1 - perform PLOGI.");
71
72 int ql2xloginretrycount;
73 module_param(ql2xloginretrycount, int, S_IRUGO);
74 MODULE_PARM_DESC(ql2xloginretrycount,
75 "Specify an alternate value for the NVRAM login retry count.");
76
77 int ql2xallocfwdump = 1;
78 module_param(ql2xallocfwdump, int, S_IRUGO);
79 MODULE_PARM_DESC(ql2xallocfwdump,
80 "Option to enable allocation of memory for a firmware dump "
81 "during HBA initialization. Memory allocation requirements "
82 "vary by ISP type. Default is 1 - allocate memory.");
83
84 int ql2xextended_error_logging;
85 module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
86 module_param_named(logging, ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
87 MODULE_PARM_DESC(ql2xextended_error_logging,
88 "Option to enable extended error logging,\n"
89 "\t\tDefault is 0 - no logging. 0x40000000 - Module Init & Probe.\n"
90 "\t\t0x20000000 - Mailbox Cmnds. 0x10000000 - Device Discovery.\n"
91 "\t\t0x08000000 - IO tracing. 0x04000000 - DPC Thread.\n"
92 "\t\t0x02000000 - Async events. 0x01000000 - Timer routines.\n"
93 "\t\t0x00800000 - User space. 0x00400000 - Task Management.\n"
94 "\t\t0x00200000 - AER/EEH. 0x00100000 - Multi Q.\n"
95 "\t\t0x00080000 - P3P Specific. 0x00040000 - Virtual Port.\n"
96 "\t\t0x00020000 - Buffer Dump. 0x00010000 - Misc.\n"
97 "\t\t0x00008000 - Verbose. 0x00004000 - Target.\n"
98 "\t\t0x00002000 - Target Mgmt. 0x00001000 - Target TMF.\n"
99 "\t\t0x7fffffff - For enabling all logs, can be too many logs.\n"
100 "\t\t0x1e400000 - Preferred value for capturing essential "
101 "debug information (equivalent to old "
102 "ql2xextended_error_logging=1).\n"
103 "\t\tDo LOGICAL OR of the value to enable more than one level");
104
105 int ql2xshiftctondsd = 6;
106 module_param(ql2xshiftctondsd, int, S_IRUGO);
107 MODULE_PARM_DESC(ql2xshiftctondsd,
108 "Set to control shifting of command type processing "
109 "based on total number of SG elements.");
110
111 int ql2xfdmienable = 1;
112 module_param(ql2xfdmienable, int, S_IRUGO|S_IWUSR);
113 module_param_named(fdmi, ql2xfdmienable, int, S_IRUGO|S_IWUSR);
114 MODULE_PARM_DESC(ql2xfdmienable,
115 "Enables FDMI registrations. "
116 "0 - no FDMI. Default is 1 - perform FDMI.");
117
118 #define MAX_Q_DEPTH 64
119 static int ql2xmaxqdepth = MAX_Q_DEPTH;
120 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
121 MODULE_PARM_DESC(ql2xmaxqdepth,
122 "Maximum queue depth to set for each LUN. "
123 "Default is 64.");
124
125 #if (IS_ENABLED(CONFIG_NVME_FC))
126 int ql2xenabledif;
127 #else
128 int ql2xenabledif = 2;
129 #endif
130 module_param(ql2xenabledif, int, S_IRUGO);
131 MODULE_PARM_DESC(ql2xenabledif,
132 " Enable T10-CRC-DIF:\n"
133 " Default is 2.\n"
134 " 0 -- No DIF Support\n"
135 " 1 -- Enable DIF for all types\n"
136 " 2 -- Enable DIF for all types, except Type 0.\n");
137
138 #if (IS_ENABLED(CONFIG_NVME_FC))
139 int ql2xnvmeenable = 1;
140 #else
141 int ql2xnvmeenable;
142 #endif
143 module_param(ql2xnvmeenable, int, 0644);
144 MODULE_PARM_DESC(ql2xnvmeenable,
145 "Enables NVME support. "
146 "0 - no NVMe. Default is Y");
147
148 int ql2xenablehba_err_chk = 2;
149 module_param(ql2xenablehba_err_chk, int, S_IRUGO|S_IWUSR);
150 MODULE_PARM_DESC(ql2xenablehba_err_chk,
151 " Enable T10-CRC-DIF Error isolation by HBA:\n"
152 " Default is 2.\n"
153 " 0 -- Error isolation disabled\n"
154 " 1 -- Error isolation enabled only for DIX Type 0\n"
155 " 2 -- Error isolation enabled for all Types\n");
156
157 int ql2xiidmaenable = 1;
158 module_param(ql2xiidmaenable, int, S_IRUGO);
159 MODULE_PARM_DESC(ql2xiidmaenable,
160 "Enables iIDMA settings "
161 "Default is 1 - perform iIDMA. 0 - no iIDMA.");
162
163 int ql2xmqsupport = 1;
164 module_param(ql2xmqsupport, int, S_IRUGO);
165 MODULE_PARM_DESC(ql2xmqsupport,
166 "Enable on demand multiple queue pairs support "
167 "Default is 1 for supported. "
168 "Set it to 0 to turn off mq qpair support.");
169
170 int ql2xfwloadbin;
171 module_param(ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
172 module_param_named(fwload, ql2xfwloadbin, int, S_IRUGO|S_IWUSR);
173 MODULE_PARM_DESC(ql2xfwloadbin,
174 "Option to specify location from which to load ISP firmware:.\n"
175 " 2 -- load firmware via the request_firmware() (hotplug).\n"
176 " interface.\n"
177 " 1 -- load firmware from flash.\n"
178 " 0 -- use default semantics.\n");
179
180 int ql2xetsenable;
181 module_param(ql2xetsenable, int, S_IRUGO);
182 MODULE_PARM_DESC(ql2xetsenable,
183 "Enables firmware ETS burst."
184 "Default is 0 - skip ETS enablement.");
185
186 int ql2xdbwr = 1;
187 module_param(ql2xdbwr, int, S_IRUGO|S_IWUSR);
188 MODULE_PARM_DESC(ql2xdbwr,
189 "Option to specify scheme for request queue posting.\n"
190 " 0 -- Regular doorbell.\n"
191 " 1 -- CAMRAM doorbell (faster).\n");
192
193 int ql2xtargetreset = 1;
194 module_param(ql2xtargetreset, int, S_IRUGO);
195 MODULE_PARM_DESC(ql2xtargetreset,
196 "Enable target reset."
197 "Default is 1 - use hw defaults.");
198
199 int ql2xgffidenable;
200 module_param(ql2xgffidenable, int, S_IRUGO);
201 MODULE_PARM_DESC(ql2xgffidenable,
202 "Enables GFF_ID checks of port type. "
203 "Default is 0 - Do not use GFF_ID information.");
204
205 int ql2xasynctmfenable = 1;
206 module_param(ql2xasynctmfenable, int, S_IRUGO);
207 MODULE_PARM_DESC(ql2xasynctmfenable,
208 "Enables issue of TM IOCBs asynchronously via IOCB mechanism"
209 "Default is 1 - Issue TM IOCBs via mailbox mechanism.");
210
211 int ql2xdontresethba;
212 module_param(ql2xdontresethba, int, S_IRUGO|S_IWUSR);
213 MODULE_PARM_DESC(ql2xdontresethba,
214 "Option to specify reset behaviour.\n"
215 " 0 (Default) -- Reset on failure.\n"
216 " 1 -- Do not reset on failure.\n");
217
218 uint64_t ql2xmaxlun = MAX_LUNS;
219 module_param(ql2xmaxlun, ullong, S_IRUGO);
220 MODULE_PARM_DESC(ql2xmaxlun,
221 "Defines the maximum LU number to register with the SCSI "
222 "midlayer. Default is 65535.");
223
224 int ql2xmdcapmask = 0x1F;
225 module_param(ql2xmdcapmask, int, S_IRUGO);
226 MODULE_PARM_DESC(ql2xmdcapmask,
227 "Set the Minidump driver capture mask level. "
228 "Default is 0x1F - Can be set to 0x3, 0x7, 0xF, 0x1F, 0x7F.");
229
230 int ql2xmdenable = 1;
231 module_param(ql2xmdenable, int, S_IRUGO);
232 MODULE_PARM_DESC(ql2xmdenable,
233 "Enable/disable MiniDump. "
234 "0 - MiniDump disabled. "
235 "1 (Default) - MiniDump enabled.");
236
237 int ql2xexlogins;
238 module_param(ql2xexlogins, uint, S_IRUGO|S_IWUSR);
239 MODULE_PARM_DESC(ql2xexlogins,
240 "Number of extended Logins. "
241 "0 (Default)- Disabled.");
242
243 int ql2xexchoffld = 1024;
244 module_param(ql2xexchoffld, uint, 0644);
245 MODULE_PARM_DESC(ql2xexchoffld,
246 "Number of target exchanges.");
247
248 int ql2xiniexchg = 1024;
249 module_param(ql2xiniexchg, uint, 0644);
250 MODULE_PARM_DESC(ql2xiniexchg,
251 "Number of initiator exchanges.");
252
253 int ql2xfwholdabts;
254 module_param(ql2xfwholdabts, int, S_IRUGO);
255 MODULE_PARM_DESC(ql2xfwholdabts,
256 "Allow FW to hold status IOCB until ABTS rsp received. "
257 "0 (Default) Do not set fw option. "
258 "1 - Set fw option to hold ABTS.");
259
260 int ql2xmvasynctoatio = 1;
261 module_param(ql2xmvasynctoatio, int, S_IRUGO|S_IWUSR);
262 MODULE_PARM_DESC(ql2xmvasynctoatio,
263 "Move PUREX, ABTS RX and RIDA IOCBs to ATIOQ"
264 "0 (Default). Do not move IOCBs"
265 "1 - Move IOCBs.");
266
267 int ql2xautodetectsfp = 1;
268 module_param(ql2xautodetectsfp, int, 0444);
269 MODULE_PARM_DESC(ql2xautodetectsfp,
270 "Detect SFP range and set appropriate distance.\n"
271 "1 (Default): Enable\n");
272
273 int ql2xenablemsix = 1;
274 module_param(ql2xenablemsix, int, 0444);
275 MODULE_PARM_DESC(ql2xenablemsix,
276 "Set to enable MSI or MSI-X interrupt mechanism.\n"
277 " Default is 1, enable MSI-X interrupt mechanism.\n"
278 " 0 -- enable traditional pin-based mechanism.\n"
279 " 1 -- enable MSI-X interrupt mechanism.\n"
280 " 2 -- enable MSI interrupt mechanism.\n");
281
282 int qla2xuseresexchforels;
283 module_param(qla2xuseresexchforels, int, 0444);
284 MODULE_PARM_DESC(qla2xuseresexchforels,
285 "Reserve 1/2 of emergency exchanges for ELS.\n"
286 " 0 (default): disabled");
287
288 static int ql2xprotmask;
289 module_param(ql2xprotmask, int, 0644);
290 MODULE_PARM_DESC(ql2xprotmask,
291 "Override DIF/DIX protection capabilities mask\n"
292 "Default is 0 which sets protection mask based on "
293 "capabilities reported by HBA firmware.\n");
294
295 static int ql2xprotguard;
296 module_param(ql2xprotguard, int, 0644);
297 MODULE_PARM_DESC(ql2xprotguard, "Override choice of DIX checksum\n"
298 " 0 -- Let HBA firmware decide\n"
299 " 1 -- Force T10 CRC\n"
300 " 2 -- Force IP checksum\n");
301
302 int ql2xdifbundlinginternalbuffers;
303 module_param(ql2xdifbundlinginternalbuffers, int, 0644);
304 MODULE_PARM_DESC(ql2xdifbundlinginternalbuffers,
305 "Force using internal buffers for DIF information\n"
306 "0 (Default). Based on check.\n"
307 "1 Force using internal buffers\n");
308
309 static void qla2x00_clear_drv_active(struct qla_hw_data *);
310 static void qla2x00_free_device(scsi_qla_host_t *);
311 static int qla2xxx_map_queues(struct Scsi_Host *shost);
312 static void qla2x00_destroy_deferred_work(struct qla_hw_data *);
313
314
315 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
316 struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
317
318 /* TODO Convert to inlines
319 *
320 * Timer routines
321 */
322
323 __inline__ void
324 qla2x00_start_timer(scsi_qla_host_t *vha, unsigned long interval)
325 {
326 timer_setup(&vha->timer, qla2x00_timer, 0);
327 vha->timer.expires = jiffies + interval * HZ;
328 add_timer(&vha->timer);
329 vha->timer_active = 1;
330 }
331
332 static inline void
333 qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
334 {
335 /* Currently used for 82XX only. */
336 if (vha->device_flags & DFLG_DEV_FAILED) {
337 ql_dbg(ql_dbg_timer, vha, 0x600d,
338 "Device in a failed state, returning.\n");
339 return;
340 }
341
342 mod_timer(&vha->timer, jiffies + interval * HZ);
343 }
344
345 static __inline__ void
346 qla2x00_stop_timer(scsi_qla_host_t *vha)
347 {
348 del_timer_sync(&vha->timer);
349 vha->timer_active = 0;
350 }
351
352 static int qla2x00_do_dpc(void *data);
353
354 static void qla2x00_rst_aen(scsi_qla_host_t *);
355
356 static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
357 struct req_que **, struct rsp_que **);
358 static void qla2x00_free_fw_dump(struct qla_hw_data *);
359 static void qla2x00_mem_free(struct qla_hw_data *);
360 int qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
361 struct qla_qpair *qpair);
362
363 /* -------------------------------------------------------------------------- */
364 static void qla_init_base_qpair(struct scsi_qla_host *vha, struct req_que *req,
365 struct rsp_que *rsp)
366 {
367 struct qla_hw_data *ha = vha->hw;
368
369 rsp->qpair = ha->base_qpair;
370 rsp->req = req;
371 ha->base_qpair->hw = ha;
372 ha->base_qpair->req = req;
373 ha->base_qpair->rsp = rsp;
374 ha->base_qpair->vha = vha;
375 ha->base_qpair->qp_lock_ptr = &ha->hardware_lock;
376 ha->base_qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
377 ha->base_qpair->msix = &ha->msix_entries[QLA_MSIX_RSP_Q];
378 ha->base_qpair->srb_mempool = ha->srb_mempool;
379 INIT_LIST_HEAD(&ha->base_qpair->hints_list);
380 ha->base_qpair->enable_class_2 = ql2xenableclass2;
381 /* init qpair to this cpu. Will adjust at run time. */
382 qla_cpu_update(rsp->qpair, raw_smp_processor_id());
383 ha->base_qpair->pdev = ha->pdev;
384
385 if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))
386 ha->base_qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
387 }
388
389 static int qla2x00_alloc_queues(struct qla_hw_data *ha, struct req_que *req,
390 struct rsp_que *rsp)
391 {
392 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
393
394 ha->req_q_map = kcalloc(ha->max_req_queues, sizeof(struct req_que *),
395 GFP_KERNEL);
396 if (!ha->req_q_map) {
397 ql_log(ql_log_fatal, vha, 0x003b,
398 "Unable to allocate memory for request queue ptrs.\n");
399 goto fail_req_map;
400 }
401
402 ha->rsp_q_map = kcalloc(ha->max_rsp_queues, sizeof(struct rsp_que *),
403 GFP_KERNEL);
404 if (!ha->rsp_q_map) {
405 ql_log(ql_log_fatal, vha, 0x003c,
406 "Unable to allocate memory for response queue ptrs.\n");
407 goto fail_rsp_map;
408 }
409
410 ha->base_qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
411 if (ha->base_qpair == NULL) {
412 ql_log(ql_log_warn, vha, 0x00e0,
413 "Failed to allocate base queue pair memory.\n");
414 goto fail_base_qpair;
415 }
416
417 qla_init_base_qpair(vha, req, rsp);
418
419 if ((ql2xmqsupport || ql2xnvmeenable) && ha->max_qpairs) {
420 ha->queue_pair_map = kcalloc(ha->max_qpairs, sizeof(struct qla_qpair *),
421 GFP_KERNEL);
422 if (!ha->queue_pair_map) {
423 ql_log(ql_log_fatal, vha, 0x0180,
424 "Unable to allocate memory for queue pair ptrs.\n");
425 goto fail_qpair_map;
426 }
427 }
428
429 /*
430 * Make sure we record at least the request and response queue zero in
431 * case we need to free them if part of the probe fails.
432 */
433 ha->rsp_q_map[0] = rsp;
434 ha->req_q_map[0] = req;
435 set_bit(0, ha->rsp_qid_map);
436 set_bit(0, ha->req_qid_map);
437 return 0;
438
439 fail_qpair_map:
440 kfree(ha->base_qpair);
441 ha->base_qpair = NULL;
442 fail_base_qpair:
443 kfree(ha->rsp_q_map);
444 ha->rsp_q_map = NULL;
445 fail_rsp_map:
446 kfree(ha->req_q_map);
447 ha->req_q_map = NULL;
448 fail_req_map:
449 return -ENOMEM;
450 }
451
452 static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
453 {
454 if (IS_QLAFX00(ha)) {
455 if (req && req->ring_fx00)
456 dma_free_coherent(&ha->pdev->dev,
457 (req->length_fx00 + 1) * sizeof(request_t),
458 req->ring_fx00, req->dma_fx00);
459 } else if (req && req->ring)
460 dma_free_coherent(&ha->pdev->dev,
461 (req->length + 1) * sizeof(request_t),
462 req->ring, req->dma);
463
464 if (req)
465 kfree(req->outstanding_cmds);
466
467 kfree(req);
468 }
469
470 static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
471 {
472 if (IS_QLAFX00(ha)) {
473 if (rsp && rsp->ring_fx00)
474 dma_free_coherent(&ha->pdev->dev,
475 (rsp->length_fx00 + 1) * sizeof(request_t),
476 rsp->ring_fx00, rsp->dma_fx00);
477 } else if (rsp && rsp->ring) {
478 dma_free_coherent(&ha->pdev->dev,
479 (rsp->length + 1) * sizeof(response_t),
480 rsp->ring, rsp->dma);
481 }
482 kfree(rsp);
483 }
484
485 static void qla2x00_free_queues(struct qla_hw_data *ha)
486 {
487 struct req_que *req;
488 struct rsp_que *rsp;
489 int cnt;
490 unsigned long flags;
491
492 if (ha->queue_pair_map) {
493 kfree(ha->queue_pair_map);
494 ha->queue_pair_map = NULL;
495 }
496 if (ha->base_qpair) {
497 kfree(ha->base_qpair);
498 ha->base_qpair = NULL;
499 }
500
501 spin_lock_irqsave(&ha->hardware_lock, flags);
502 for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
503 if (!test_bit(cnt, ha->req_qid_map))
504 continue;
505
506 req = ha->req_q_map[cnt];
507 clear_bit(cnt, ha->req_qid_map);
508 ha->req_q_map[cnt] = NULL;
509
510 spin_unlock_irqrestore(&ha->hardware_lock, flags);
511 qla2x00_free_req_que(ha, req);
512 spin_lock_irqsave(&ha->hardware_lock, flags);
513 }
514 spin_unlock_irqrestore(&ha->hardware_lock, flags);
515
516 kfree(ha->req_q_map);
517 ha->req_q_map = NULL;
518
519
520 spin_lock_irqsave(&ha->hardware_lock, flags);
521 for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
522 if (!test_bit(cnt, ha->rsp_qid_map))
523 continue;
524
525 rsp = ha->rsp_q_map[cnt];
526 clear_bit(cnt, ha->rsp_qid_map);
527 ha->rsp_q_map[cnt] = NULL;
528 spin_unlock_irqrestore(&ha->hardware_lock, flags);
529 qla2x00_free_rsp_que(ha, rsp);
530 spin_lock_irqsave(&ha->hardware_lock, flags);
531 }
532 spin_unlock_irqrestore(&ha->hardware_lock, flags);
533
534 kfree(ha->rsp_q_map);
535 ha->rsp_q_map = NULL;
536 }
537
538 static char *
539 qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str, size_t str_len)
540 {
541 struct qla_hw_data *ha = vha->hw;
542 static const char *const pci_bus_modes[] = {
543 "33", "66", "100", "133",
544 };
545 uint16_t pci_bus;
546
547 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
548 if (pci_bus) {
549 snprintf(str, str_len, "PCI-X (%s MHz)",
550 pci_bus_modes[pci_bus]);
551 } else {
552 pci_bus = (ha->pci_attr & BIT_8) >> 8;
553 snprintf(str, str_len, "PCI (%s MHz)", pci_bus_modes[pci_bus]);
554 }
555
556 return str;
557 }
558
559 static char *
560 qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str, size_t str_len)
561 {
562 static const char *const pci_bus_modes[] = {
563 "33", "66", "100", "133",
564 };
565 struct qla_hw_data *ha = vha->hw;
566 uint32_t pci_bus;
567
568 if (pci_is_pcie(ha->pdev)) {
569 uint32_t lstat, lspeed, lwidth;
570 const char *speed_str;
571
572 pcie_capability_read_dword(ha->pdev, PCI_EXP_LNKCAP, &lstat);
573 lspeed = lstat & PCI_EXP_LNKCAP_SLS;
574 lwidth = (lstat & PCI_EXP_LNKCAP_MLW) >> 4;
575
576 switch (lspeed) {
577 case 1:
578 speed_str = "2.5GT/s";
579 break;
580 case 2:
581 speed_str = "5.0GT/s";
582 break;
583 case 3:
584 speed_str = "8.0GT/s";
585 break;
586 default:
587 speed_str = "<unknown>";
588 break;
589 }
590 snprintf(str, str_len, "PCIe (%s x%d)", speed_str, lwidth);
591
592 return str;
593 }
594
595 pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
596 if (pci_bus == 0 || pci_bus == 8)
597 snprintf(str, str_len, "PCI (%s MHz)",
598 pci_bus_modes[pci_bus >> 3]);
599 else
600 snprintf(str, str_len, "PCI-X Mode %d (%s MHz)",
601 pci_bus & 4 ? 2 : 1,
602 pci_bus_modes[pci_bus & 3]);
603
604 return str;
605 }
606
607 static char *
608 qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
609 {
610 char un_str[10];
611 struct qla_hw_data *ha = vha->hw;
612
613 snprintf(str, size, "%d.%02d.%02d ", ha->fw_major_version,
614 ha->fw_minor_version, ha->fw_subminor_version);
615
616 if (ha->fw_attributes & BIT_9) {
617 strcat(str, "FLX");
618 return (str);
619 }
620
621 switch (ha->fw_attributes & 0xFF) {
622 case 0x7:
623 strcat(str, "EF");
624 break;
625 case 0x17:
626 strcat(str, "TP");
627 break;
628 case 0x37:
629 strcat(str, "IP");
630 break;
631 case 0x77:
632 strcat(str, "VI");
633 break;
634 default:
635 sprintf(un_str, "(%x)", ha->fw_attributes);
636 strcat(str, un_str);
637 break;
638 }
639 if (ha->fw_attributes & 0x100)
640 strcat(str, "X");
641
642 return (str);
643 }
644
645 static char *
646 qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str, size_t size)
647 {
648 struct qla_hw_data *ha = vha->hw;
649
650 snprintf(str, size, "%d.%02d.%02d (%x)", ha->fw_major_version,
651 ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
652 return str;
653 }
654
655 void qla2x00_sp_free_dma(srb_t *sp)
656 {
657 struct qla_hw_data *ha = sp->vha->hw;
658 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
659 void *ctx = GET_CMD_CTX_SP(sp);
660
661 if (sp->flags & SRB_DMA_VALID) {
662 scsi_dma_unmap(cmd);
663 sp->flags &= ~SRB_DMA_VALID;
664 }
665
666 if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
667 dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
668 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
669 sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
670 }
671
672 if (!ctx)
673 return;
674
675 if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
676 /* List assured to be having elements */
677 qla2x00_clean_dsd_pool(ha, ctx);
678 sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
679 }
680
681 if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
682 struct crc_context *ctx0 = ctx;
683
684 dma_pool_free(ha->dl_dma_pool, ctx0, ctx0->crc_ctx_dma);
685 sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
686 }
687
688 if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
689 struct ct6_dsd *ctx1 = ctx;
690
691 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
692 ctx1->fcp_cmnd_dma);
693 list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
694 ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
695 ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
696 mempool_free(ctx1, ha->ctx_mempool);
697 }
698 }
699
700 void qla2x00_sp_compl(srb_t *sp, int res)
701 {
702 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
703 struct completion *comp = sp->comp;
704
705 if (WARN_ON_ONCE(atomic_read(&sp->ref_count) == 0))
706 return;
707
708 atomic_dec(&sp->ref_count);
709
710 sp->free(sp);
711 cmd->result = res;
712 CMD_SP(cmd) = NULL;
713 cmd->scsi_done(cmd);
714 if (comp)
715 complete(comp);
716 }
717
718 void qla2xxx_qpair_sp_free_dma(srb_t *sp)
719 {
720 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
721 struct qla_hw_data *ha = sp->fcport->vha->hw;
722 void *ctx = GET_CMD_CTX_SP(sp);
723
724 if (sp->flags & SRB_DMA_VALID) {
725 scsi_dma_unmap(cmd);
726 sp->flags &= ~SRB_DMA_VALID;
727 }
728
729 if (sp->flags & SRB_CRC_PROT_DMA_VALID) {
730 dma_unmap_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
731 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
732 sp->flags &= ~SRB_CRC_PROT_DMA_VALID;
733 }
734
735 if (!ctx)
736 return;
737
738 if (sp->flags & SRB_CRC_CTX_DSD_VALID) {
739 /* List assured to be having elements */
740 qla2x00_clean_dsd_pool(ha, ctx);
741 sp->flags &= ~SRB_CRC_CTX_DSD_VALID;
742 }
743
744 if (sp->flags & SRB_DIF_BUNDL_DMA_VALID) {
745 struct crc_context *difctx = ctx;
746 struct dsd_dma *dif_dsd, *nxt_dsd;
747
748 list_for_each_entry_safe(dif_dsd, nxt_dsd,
749 &difctx->ldif_dma_hndl_list, list) {
750 list_del(&dif_dsd->list);
751 dma_pool_free(ha->dif_bundl_pool, dif_dsd->dsd_addr,
752 dif_dsd->dsd_list_dma);
753 kfree(dif_dsd);
754 difctx->no_dif_bundl--;
755 }
756
757 list_for_each_entry_safe(dif_dsd, nxt_dsd,
758 &difctx->ldif_dsd_list, list) {
759 list_del(&dif_dsd->list);
760 dma_pool_free(ha->dl_dma_pool, dif_dsd->dsd_addr,
761 dif_dsd->dsd_list_dma);
762 kfree(dif_dsd);
763 difctx->no_ldif_dsd--;
764 }
765
766 if (difctx->no_ldif_dsd) {
767 ql_dbg(ql_dbg_tgt+ql_dbg_verbose, sp->vha, 0xe022,
768 "%s: difctx->no_ldif_dsd=%x\n",
769 __func__, difctx->no_ldif_dsd);
770 }
771
772 if (difctx->no_dif_bundl) {
773 ql_dbg(ql_dbg_tgt+ql_dbg_verbose, sp->vha, 0xe022,
774 "%s: difctx->no_dif_bundl=%x\n",
775 __func__, difctx->no_dif_bundl);
776 }
777 sp->flags &= ~SRB_DIF_BUNDL_DMA_VALID;
778 }
779
780 if (sp->flags & SRB_FCP_CMND_DMA_VALID) {
781 struct ct6_dsd *ctx1 = ctx;
782
783 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx1->fcp_cmnd,
784 ctx1->fcp_cmnd_dma);
785 list_splice(&ctx1->dsd_list, &ha->gbl_dsd_list);
786 ha->gbl_dsd_inuse -= ctx1->dsd_use_cnt;
787 ha->gbl_dsd_avail += ctx1->dsd_use_cnt;
788 mempool_free(ctx1, ha->ctx_mempool);
789 sp->flags &= ~SRB_FCP_CMND_DMA_VALID;
790 }
791
792 if (sp->flags & SRB_CRC_CTX_DMA_VALID) {
793 struct crc_context *ctx0 = ctx;
794
795 dma_pool_free(ha->dl_dma_pool, ctx, ctx0->crc_ctx_dma);
796 sp->flags &= ~SRB_CRC_CTX_DMA_VALID;
797 }
798 }
799
800 void qla2xxx_qpair_sp_compl(srb_t *sp, int res)
801 {
802 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
803 struct completion *comp = sp->comp;
804
805 if (WARN_ON_ONCE(atomic_read(&sp->ref_count) == 0))
806 return;
807
808 atomic_dec(&sp->ref_count);
809
810 sp->free(sp);
811 cmd->result = res;
812 CMD_SP(cmd) = NULL;
813 cmd->scsi_done(cmd);
814 if (comp)
815 complete(comp);
816 }
817
818 static int
819 qla2xxx_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
820 {
821 scsi_qla_host_t *vha = shost_priv(host);
822 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
823 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
824 struct qla_hw_data *ha = vha->hw;
825 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
826 srb_t *sp;
827 int rval;
828
829 if (unlikely(test_bit(UNLOADING, &base_vha->dpc_flags)) ||
830 WARN_ON_ONCE(!rport)) {
831 cmd->result = DID_NO_CONNECT << 16;
832 goto qc24_fail_command;
833 }
834
835 if (ha->mqenable) {
836 uint32_t tag;
837 uint16_t hwq;
838 struct qla_qpair *qpair = NULL;
839
840 tag = blk_mq_unique_tag(cmd->request);
841 hwq = blk_mq_unique_tag_to_hwq(tag);
842 qpair = ha->queue_pair_map[hwq];
843
844 if (qpair)
845 return qla2xxx_mqueuecommand(host, cmd, qpair);
846 }
847
848 if (ha->flags.eeh_busy) {
849 if (ha->flags.pci_channel_io_perm_failure) {
850 ql_dbg(ql_dbg_aer, vha, 0x9010,
851 "PCI Channel IO permanent failure, exiting "
852 "cmd=%p.\n", cmd);
853 cmd->result = DID_NO_CONNECT << 16;
854 } else {
855 ql_dbg(ql_dbg_aer, vha, 0x9011,
856 "EEH_Busy, Requeuing the cmd=%p.\n", cmd);
857 cmd->result = DID_REQUEUE << 16;
858 }
859 goto qc24_fail_command;
860 }
861
862 rval = fc_remote_port_chkready(rport);
863 if (rval) {
864 cmd->result = rval;
865 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3003,
866 "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
867 cmd, rval);
868 goto qc24_fail_command;
869 }
870
871 if (!vha->flags.difdix_supported &&
872 scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
873 ql_dbg(ql_dbg_io, vha, 0x3004,
874 "DIF Cap not reg, fail DIF capable cmd's:%p.\n",
875 cmd);
876 cmd->result = DID_NO_CONNECT << 16;
877 goto qc24_fail_command;
878 }
879
880 if (!fcport) {
881 cmd->result = DID_NO_CONNECT << 16;
882 goto qc24_fail_command;
883 }
884
885 if (atomic_read(&fcport->state) != FCS_ONLINE) {
886 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
887 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
888 ql_dbg(ql_dbg_io, vha, 0x3005,
889 "Returning DNC, fcport_state=%d loop_state=%d.\n",
890 atomic_read(&fcport->state),
891 atomic_read(&base_vha->loop_state));
892 cmd->result = DID_NO_CONNECT << 16;
893 goto qc24_fail_command;
894 }
895 goto qc24_target_busy;
896 }
897
898 /*
899 * Return target busy if we've received a non-zero retry_delay_timer
900 * in a FCP_RSP.
901 */
902 if (fcport->retry_delay_timestamp == 0) {
903 /* retry delay not set */
904 } else if (time_after(jiffies, fcport->retry_delay_timestamp))
905 fcport->retry_delay_timestamp = 0;
906 else
907 goto qc24_target_busy;
908
909 sp = scsi_cmd_priv(cmd);
910 qla2xxx_init_sp(sp, vha, vha->hw->base_qpair, fcport);
911
912 sp->u.scmd.cmd = cmd;
913 sp->type = SRB_SCSI_CMD;
914 atomic_set(&sp->ref_count, 1);
915 CMD_SP(cmd) = (void *)sp;
916 sp->free = qla2x00_sp_free_dma;
917 sp->done = qla2x00_sp_compl;
918
919 rval = ha->isp_ops->start_scsi(sp);
920 if (rval != QLA_SUCCESS) {
921 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3013,
922 "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
923 goto qc24_host_busy_free_sp;
924 }
925
926 return 0;
927
928 qc24_host_busy_free_sp:
929 sp->free(sp);
930
931 qc24_target_busy:
932 return SCSI_MLQUEUE_TARGET_BUSY;
933
934 qc24_fail_command:
935 cmd->scsi_done(cmd);
936
937 return 0;
938 }
939
940 /* For MQ supported I/O */
941 int
942 qla2xxx_mqueuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd,
943 struct qla_qpair *qpair)
944 {
945 scsi_qla_host_t *vha = shost_priv(host);
946 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
947 struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
948 struct qla_hw_data *ha = vha->hw;
949 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
950 srb_t *sp;
951 int rval;
952
953 rval = rport ? fc_remote_port_chkready(rport) : FC_PORTSTATE_OFFLINE;
954 if (rval) {
955 cmd->result = rval;
956 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3076,
957 "fc_remote_port_chkready failed for cmd=%p, rval=0x%x.\n",
958 cmd, rval);
959 goto qc24_fail_command;
960 }
961
962 if (!fcport) {
963 cmd->result = DID_NO_CONNECT << 16;
964 goto qc24_fail_command;
965 }
966
967 if (atomic_read(&fcport->state) != FCS_ONLINE) {
968 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
969 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
970 ql_dbg(ql_dbg_io, vha, 0x3077,
971 "Returning DNC, fcport_state=%d loop_state=%d.\n",
972 atomic_read(&fcport->state),
973 atomic_read(&base_vha->loop_state));
974 cmd->result = DID_NO_CONNECT << 16;
975 goto qc24_fail_command;
976 }
977 goto qc24_target_busy;
978 }
979
980 /*
981 * Return target busy if we've received a non-zero retry_delay_timer
982 * in a FCP_RSP.
983 */
984 if (fcport->retry_delay_timestamp == 0) {
985 /* retry delay not set */
986 } else if (time_after(jiffies, fcport->retry_delay_timestamp))
987 fcport->retry_delay_timestamp = 0;
988 else
989 goto qc24_target_busy;
990
991 sp = scsi_cmd_priv(cmd);
992 qla2xxx_init_sp(sp, vha, qpair, fcport);
993
994 sp->u.scmd.cmd = cmd;
995 sp->type = SRB_SCSI_CMD;
996 atomic_set(&sp->ref_count, 1);
997 CMD_SP(cmd) = (void *)sp;
998 sp->free = qla2xxx_qpair_sp_free_dma;
999 sp->done = qla2xxx_qpair_sp_compl;
1000 sp->qpair = qpair;
1001
1002 rval = ha->isp_ops->start_scsi_mq(sp);
1003 if (rval != QLA_SUCCESS) {
1004 ql_dbg(ql_dbg_io + ql_dbg_verbose, vha, 0x3078,
1005 "Start scsi failed rval=%d for cmd=%p.\n", rval, cmd);
1006 if (rval == QLA_INTERFACE_ERROR)
1007 goto qc24_fail_command;
1008 goto qc24_host_busy_free_sp;
1009 }
1010
1011 return 0;
1012
1013 qc24_host_busy_free_sp:
1014 sp->free(sp);
1015
1016 qc24_target_busy:
1017 return SCSI_MLQUEUE_TARGET_BUSY;
1018
1019 qc24_fail_command:
1020 cmd->scsi_done(cmd);
1021
1022 return 0;
1023 }
1024
1025 /*
1026 * qla2x00_eh_wait_on_command
1027 * Waits for the command to be returned by the Firmware for some
1028 * max time.
1029 *
1030 * Input:
1031 * cmd = Scsi Command to wait on.
1032 *
1033 * Return:
1034 * Completed in time : QLA_SUCCESS
1035 * Did not complete in time : QLA_FUNCTION_FAILED
1036 */
1037 static int
1038 qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
1039 {
1040 #define ABORT_POLLING_PERIOD 1000
1041 #define ABORT_WAIT_ITER ((2 * 1000) / (ABORT_POLLING_PERIOD))
1042 unsigned long wait_iter = ABORT_WAIT_ITER;
1043 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1044 struct qla_hw_data *ha = vha->hw;
1045 int ret = QLA_SUCCESS;
1046
1047 if (unlikely(pci_channel_offline(ha->pdev)) || ha->flags.eeh_busy) {
1048 ql_dbg(ql_dbg_taskm, vha, 0x8005,
1049 "Return:eh_wait.\n");
1050 return ret;
1051 }
1052
1053 while (CMD_SP(cmd) && wait_iter--) {
1054 msleep(ABORT_POLLING_PERIOD);
1055 }
1056 if (CMD_SP(cmd))
1057 ret = QLA_FUNCTION_FAILED;
1058
1059 return ret;
1060 }
1061
1062 /*
1063 * qla2x00_wait_for_hba_online
1064 * Wait till the HBA is online after going through
1065 * <= MAX_RETRIES_OF_ISP_ABORT or
1066 * finally HBA is disabled ie marked offline
1067 *
1068 * Input:
1069 * ha - pointer to host adapter structure
1070 *
1071 * Note:
1072 * Does context switching-Release SPIN_LOCK
1073 * (if any) before calling this routine.
1074 *
1075 * Return:
1076 * Success (Adapter is online) : 0
1077 * Failed (Adapter is offline/disabled) : 1
1078 */
1079 int
1080 qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
1081 {
1082 int return_status;
1083 unsigned long wait_online;
1084 struct qla_hw_data *ha = vha->hw;
1085 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1086
1087 wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1088 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1089 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1090 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1091 ha->dpc_active) && time_before(jiffies, wait_online)) {
1092
1093 msleep(1000);
1094 }
1095 if (base_vha->flags.online)
1096 return_status = QLA_SUCCESS;
1097 else
1098 return_status = QLA_FUNCTION_FAILED;
1099
1100 return (return_status);
1101 }
1102
1103 static inline int test_fcport_count(scsi_qla_host_t *vha)
1104 {
1105 struct qla_hw_data *ha = vha->hw;
1106 unsigned long flags;
1107 int res;
1108
1109 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1110 ql_dbg(ql_dbg_init, vha, 0x00ec,
1111 "tgt %p, fcport_count=%d\n",
1112 vha, vha->fcport_count);
1113 res = (vha->fcport_count == 0);
1114 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1115
1116 return res;
1117 }
1118
1119 /*
1120 * qla2x00_wait_for_sess_deletion can only be called from remove_one.
1121 * it has dependency on UNLOADING flag to stop device discovery
1122 */
1123 void
1124 qla2x00_wait_for_sess_deletion(scsi_qla_host_t *vha)
1125 {
1126 qla2x00_mark_all_devices_lost(vha, 0);
1127
1128 wait_event_timeout(vha->fcport_waitQ, test_fcport_count(vha), 10*HZ);
1129 }
1130
1131 /*
1132 * qla2x00_wait_for_hba_ready
1133 * Wait till the HBA is ready before doing driver unload
1134 *
1135 * Input:
1136 * ha - pointer to host adapter structure
1137 *
1138 * Note:
1139 * Does context switching-Release SPIN_LOCK
1140 * (if any) before calling this routine.
1141 *
1142 */
1143 static void
1144 qla2x00_wait_for_hba_ready(scsi_qla_host_t *vha)
1145 {
1146 struct qla_hw_data *ha = vha->hw;
1147 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1148
1149 while ((qla2x00_reset_active(vha) || ha->dpc_active ||
1150 ha->flags.mbox_busy) ||
1151 test_bit(FX00_RESET_RECOVERY, &vha->dpc_flags) ||
1152 test_bit(FX00_TARGET_SCAN, &vha->dpc_flags)) {
1153 if (test_bit(UNLOADING, &base_vha->dpc_flags))
1154 break;
1155 msleep(1000);
1156 }
1157 }
1158
1159 int
1160 qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
1161 {
1162 int return_status;
1163 unsigned long wait_reset;
1164 struct qla_hw_data *ha = vha->hw;
1165 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1166
1167 wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
1168 while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
1169 test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
1170 test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
1171 ha->dpc_active) && time_before(jiffies, wait_reset)) {
1172
1173 msleep(1000);
1174
1175 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
1176 ha->flags.chip_reset_done)
1177 break;
1178 }
1179 if (ha->flags.chip_reset_done)
1180 return_status = QLA_SUCCESS;
1181 else
1182 return_status = QLA_FUNCTION_FAILED;
1183
1184 return return_status;
1185 }
1186
1187 static int
1188 sp_get(struct srb *sp)
1189 {
1190 if (!refcount_inc_not_zero((refcount_t *)&sp->ref_count))
1191 /* kref get fail */
1192 return ENXIO;
1193 else
1194 return 0;
1195 }
1196
1197 #define ISP_REG_DISCONNECT 0xffffffffU
1198 /**************************************************************************
1199 * qla2x00_isp_reg_stat
1200 *
1201 * Description:
1202 * Read the host status register of ISP before aborting the command.
1203 *
1204 * Input:
1205 * ha = pointer to host adapter structure.
1206 *
1207 *
1208 * Returns:
1209 * Either true or false.
1210 *
1211 * Note: Return true if there is register disconnect.
1212 **************************************************************************/
1213 static inline
1214 uint32_t qla2x00_isp_reg_stat(struct qla_hw_data *ha)
1215 {
1216 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1217 struct device_reg_82xx __iomem *reg82 = &ha->iobase->isp82;
1218
1219 if (IS_P3P_TYPE(ha))
1220 return ((RD_REG_DWORD(&reg82->host_int)) == ISP_REG_DISCONNECT);
1221 else
1222 return ((RD_REG_DWORD(&reg->host_status)) ==
1223 ISP_REG_DISCONNECT);
1224 }
1225
1226 /**************************************************************************
1227 * qla2xxx_eh_abort
1228 *
1229 * Description:
1230 * The abort function will abort the specified command.
1231 *
1232 * Input:
1233 * cmd = Linux SCSI command packet to be aborted.
1234 *
1235 * Returns:
1236 * Either SUCCESS or FAILED.
1237 *
1238 * Note:
1239 * Only return FAILED if command not returned by firmware.
1240 **************************************************************************/
1241 static int
1242 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
1243 {
1244 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1245 DECLARE_COMPLETION_ONSTACK(comp);
1246 srb_t *sp;
1247 int ret;
1248 unsigned int id;
1249 uint64_t lun;
1250 int rval;
1251 struct qla_hw_data *ha = vha->hw;
1252
1253 if (qla2x00_isp_reg_stat(ha)) {
1254 ql_log(ql_log_info, vha, 0x8042,
1255 "PCI/Register disconnect, exiting.\n");
1256 return FAILED;
1257 }
1258
1259 ret = fc_block_scsi_eh(cmd);
1260 if (ret != 0)
1261 return ret;
1262
1263 sp = scsi_cmd_priv(cmd);
1264
1265 if (sp->fcport && sp->fcport->deleted)
1266 return SUCCESS;
1267
1268 /* Return if the command has already finished. */
1269 if (sp_get(sp))
1270 return SUCCESS;
1271
1272 id = cmd->device->id;
1273 lun = cmd->device->lun;
1274
1275 ql_dbg(ql_dbg_taskm, vha, 0x8002,
1276 "Aborting from RISC nexus=%ld:%d:%llu sp=%p cmd=%p handle=%x\n",
1277 vha->host_no, id, lun, sp, cmd, sp->handle);
1278
1279 rval = ha->isp_ops->abort_command(sp);
1280 ql_dbg(ql_dbg_taskm, vha, 0x8003,
1281 "Abort command mbx cmd=%p, rval=%x.\n", cmd, rval);
1282
1283 switch (rval) {
1284 case QLA_SUCCESS:
1285 /*
1286 * The command has been aborted. That means that the firmware
1287 * won't report a completion.
1288 */
1289 sp->done(sp, DID_ABORT << 16);
1290 ret = SUCCESS;
1291 break;
1292 case QLA_FUNCTION_PARAMETER_ERROR: {
1293 /* Wait for the command completion. */
1294 uint32_t ratov = ha->r_a_tov/10;
1295 uint32_t ratov_j = msecs_to_jiffies(4 * ratov * 1000);
1296
1297 WARN_ON_ONCE(sp->comp);
1298 sp->comp = &comp;
1299 if (!wait_for_completion_timeout(&comp, ratov_j)) {
1300 ql_dbg(ql_dbg_taskm, vha, 0xffff,
1301 "%s: Abort wait timer (4 * R_A_TOV[%d]) expired\n",
1302 __func__, ha->r_a_tov);
1303 ret = FAILED;
1304 } else {
1305 ret = SUCCESS;
1306 }
1307 break;
1308 }
1309 default:
1310 /*
1311 * Either abort failed or abort and completion raced. Let
1312 * the SCSI core retry the abort in the former case.
1313 */
1314 ret = FAILED;
1315 break;
1316 }
1317
1318 sp->comp = NULL;
1319 atomic_dec(&sp->ref_count);
1320 ql_log(ql_log_info, vha, 0x801c,
1321 "Abort command issued nexus=%ld:%d:%llu -- %x.\n",
1322 vha->host_no, id, lun, ret);
1323
1324 return ret;
1325 }
1326
1327 /*
1328 * Returns: QLA_SUCCESS or QLA_FUNCTION_FAILED.
1329 */
1330 int
1331 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
1332 uint64_t l, enum nexus_wait_type type)
1333 {
1334 int cnt, match, status;
1335 unsigned long flags;
1336 struct qla_hw_data *ha = vha->hw;
1337 struct req_que *req;
1338 srb_t *sp;
1339 struct scsi_cmnd *cmd;
1340
1341 status = QLA_SUCCESS;
1342
1343 spin_lock_irqsave(&ha->hardware_lock, flags);
1344 req = vha->req;
1345 for (cnt = 1; status == QLA_SUCCESS &&
1346 cnt < req->num_outstanding_cmds; cnt++) {
1347 sp = req->outstanding_cmds[cnt];
1348 if (!sp)
1349 continue;
1350 if (sp->type != SRB_SCSI_CMD)
1351 continue;
1352 if (vha->vp_idx != sp->vha->vp_idx)
1353 continue;
1354 match = 0;
1355 cmd = GET_CMD_SP(sp);
1356 switch (type) {
1357 case WAIT_HOST:
1358 match = 1;
1359 break;
1360 case WAIT_TARGET:
1361 match = cmd->device->id == t;
1362 break;
1363 case WAIT_LUN:
1364 match = (cmd->device->id == t &&
1365 cmd->device->lun == l);
1366 break;
1367 }
1368 if (!match)
1369 continue;
1370
1371 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1372 status = qla2x00_eh_wait_on_command(cmd);
1373 spin_lock_irqsave(&ha->hardware_lock, flags);
1374 }
1375 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1376
1377 return status;
1378 }
1379
1380 static char *reset_errors[] = {
1381 "HBA not online",
1382 "HBA not ready",
1383 "Task management failed",
1384 "Waiting for command completions",
1385 };
1386
1387 static int
1388 __qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
1389 struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, uint64_t, int))
1390 {
1391 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1392 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1393 int err;
1394
1395 if (!fcport) {
1396 return FAILED;
1397 }
1398
1399 err = fc_block_scsi_eh(cmd);
1400 if (err != 0)
1401 return err;
1402
1403 if (fcport->deleted)
1404 return SUCCESS;
1405
1406 ql_log(ql_log_info, vha, 0x8009,
1407 "%s RESET ISSUED nexus=%ld:%d:%llu cmd=%p.\n", name, vha->host_no,
1408 cmd->device->id, cmd->device->lun, cmd);
1409
1410 err = 0;
1411 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1412 ql_log(ql_log_warn, vha, 0x800a,
1413 "Wait for hba online failed for cmd=%p.\n", cmd);
1414 goto eh_reset_failed;
1415 }
1416 err = 2;
1417 if (do_reset(fcport, cmd->device->lun, 1)
1418 != QLA_SUCCESS) {
1419 ql_log(ql_log_warn, vha, 0x800c,
1420 "do_reset failed for cmd=%p.\n", cmd);
1421 goto eh_reset_failed;
1422 }
1423 err = 3;
1424 if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
1425 cmd->device->lun, type) != QLA_SUCCESS) {
1426 ql_log(ql_log_warn, vha, 0x800d,
1427 "wait for pending cmds failed for cmd=%p.\n", cmd);
1428 goto eh_reset_failed;
1429 }
1430
1431 ql_log(ql_log_info, vha, 0x800e,
1432 "%s RESET SUCCEEDED nexus:%ld:%d:%llu cmd=%p.\n", name,
1433 vha->host_no, cmd->device->id, cmd->device->lun, cmd);
1434
1435 return SUCCESS;
1436
1437 eh_reset_failed:
1438 ql_log(ql_log_info, vha, 0x800f,
1439 "%s RESET FAILED: %s nexus=%ld:%d:%llu cmd=%p.\n", name,
1440 reset_errors[err], vha->host_no, cmd->device->id, cmd->device->lun,
1441 cmd);
1442 return FAILED;
1443 }
1444
1445 static int
1446 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
1447 {
1448 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1449 struct qla_hw_data *ha = vha->hw;
1450
1451 if (qla2x00_isp_reg_stat(ha)) {
1452 ql_log(ql_log_info, vha, 0x803e,
1453 "PCI/Register disconnect, exiting.\n");
1454 return FAILED;
1455 }
1456
1457 return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
1458 ha->isp_ops->lun_reset);
1459 }
1460
1461 static int
1462 qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
1463 {
1464 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1465 struct qla_hw_data *ha = vha->hw;
1466
1467 if (qla2x00_isp_reg_stat(ha)) {
1468 ql_log(ql_log_info, vha, 0x803f,
1469 "PCI/Register disconnect, exiting.\n");
1470 return FAILED;
1471 }
1472
1473 return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
1474 ha->isp_ops->target_reset);
1475 }
1476
1477 /**************************************************************************
1478 * qla2xxx_eh_bus_reset
1479 *
1480 * Description:
1481 * The bus reset function will reset the bus and abort any executing
1482 * commands.
1483 *
1484 * Input:
1485 * cmd = Linux SCSI command packet of the command that cause the
1486 * bus reset.
1487 *
1488 * Returns:
1489 * SUCCESS/FAILURE (defined as macro in scsi.h).
1490 *
1491 **************************************************************************/
1492 static int
1493 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
1494 {
1495 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1496 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
1497 int ret = FAILED;
1498 unsigned int id;
1499 uint64_t lun;
1500 struct qla_hw_data *ha = vha->hw;
1501
1502 if (qla2x00_isp_reg_stat(ha)) {
1503 ql_log(ql_log_info, vha, 0x8040,
1504 "PCI/Register disconnect, exiting.\n");
1505 return FAILED;
1506 }
1507
1508 id = cmd->device->id;
1509 lun = cmd->device->lun;
1510
1511 if (!fcport) {
1512 return ret;
1513 }
1514
1515 ret = fc_block_scsi_eh(cmd);
1516 if (ret != 0)
1517 return ret;
1518 ret = FAILED;
1519
1520 if (qla2x00_chip_is_down(vha))
1521 return ret;
1522
1523 ql_log(ql_log_info, vha, 0x8012,
1524 "BUS RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1525
1526 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1527 ql_log(ql_log_fatal, vha, 0x8013,
1528 "Wait for hba online failed board disabled.\n");
1529 goto eh_bus_reset_done;
1530 }
1531
1532 if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
1533 ret = SUCCESS;
1534
1535 if (ret == FAILED)
1536 goto eh_bus_reset_done;
1537
1538 /* Flush outstanding commands. */
1539 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) !=
1540 QLA_SUCCESS) {
1541 ql_log(ql_log_warn, vha, 0x8014,
1542 "Wait for pending commands failed.\n");
1543 ret = FAILED;
1544 }
1545
1546 eh_bus_reset_done:
1547 ql_log(ql_log_warn, vha, 0x802b,
1548 "BUS RESET %s nexus=%ld:%d:%llu.\n",
1549 (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1550
1551 return ret;
1552 }
1553
1554 /**************************************************************************
1555 * qla2xxx_eh_host_reset
1556 *
1557 * Description:
1558 * The reset function will reset the Adapter.
1559 *
1560 * Input:
1561 * cmd = Linux SCSI command packet of the command that cause the
1562 * adapter reset.
1563 *
1564 * Returns:
1565 * Either SUCCESS or FAILED.
1566 *
1567 * Note:
1568 **************************************************************************/
1569 static int
1570 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1571 {
1572 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
1573 struct qla_hw_data *ha = vha->hw;
1574 int ret = FAILED;
1575 unsigned int id;
1576 uint64_t lun;
1577 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1578
1579 if (qla2x00_isp_reg_stat(ha)) {
1580 ql_log(ql_log_info, vha, 0x8041,
1581 "PCI/Register disconnect, exiting.\n");
1582 schedule_work(&ha->board_disable);
1583 return SUCCESS;
1584 }
1585
1586 id = cmd->device->id;
1587 lun = cmd->device->lun;
1588
1589 ql_log(ql_log_info, vha, 0x8018,
1590 "ADAPTER RESET ISSUED nexus=%ld:%d:%llu.\n", vha->host_no, id, lun);
1591
1592 /*
1593 * No point in issuing another reset if one is active. Also do not
1594 * attempt a reset if we are updating flash.
1595 */
1596 if (qla2x00_reset_active(vha) || ha->optrom_state != QLA_SWAITING)
1597 goto eh_host_reset_lock;
1598
1599 if (vha != base_vha) {
1600 if (qla2x00_vp_abort_isp(vha))
1601 goto eh_host_reset_lock;
1602 } else {
1603 if (IS_P3P_TYPE(vha->hw)) {
1604 if (!qla82xx_fcoe_ctx_reset(vha)) {
1605 /* Ctx reset success */
1606 ret = SUCCESS;
1607 goto eh_host_reset_lock;
1608 }
1609 /* fall thru if ctx reset failed */
1610 }
1611 if (ha->wq)
1612 flush_workqueue(ha->wq);
1613
1614 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1615 if (ha->isp_ops->abort_isp(base_vha)) {
1616 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1617 /* failed. schedule dpc to try */
1618 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1619
1620 if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
1621 ql_log(ql_log_warn, vha, 0x802a,
1622 "wait for hba online failed.\n");
1623 goto eh_host_reset_lock;
1624 }
1625 }
1626 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1627 }
1628
1629 /* Waiting for command to be returned to OS.*/
1630 if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST) ==
1631 QLA_SUCCESS)
1632 ret = SUCCESS;
1633
1634 eh_host_reset_lock:
1635 ql_log(ql_log_info, vha, 0x8017,
1636 "ADAPTER RESET %s nexus=%ld:%d:%llu.\n",
1637 (ret == FAILED) ? "FAILED" : "SUCCEEDED", vha->host_no, id, lun);
1638
1639 return ret;
1640 }
1641
1642 /*
1643 * qla2x00_loop_reset
1644 * Issue loop reset.
1645 *
1646 * Input:
1647 * ha = adapter block pointer.
1648 *
1649 * Returns:
1650 * 0 = success
1651 */
1652 int
1653 qla2x00_loop_reset(scsi_qla_host_t *vha)
1654 {
1655 int ret;
1656 struct fc_port *fcport;
1657 struct qla_hw_data *ha = vha->hw;
1658
1659 if (IS_QLAFX00(ha)) {
1660 return qlafx00_loop_reset(vha);
1661 }
1662
1663 if (ql2xtargetreset == 1 && ha->flags.enable_target_reset) {
1664 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1665 if (fcport->port_type != FCT_TARGET)
1666 continue;
1667
1668 ret = ha->isp_ops->target_reset(fcport, 0, 0);
1669 if (ret != QLA_SUCCESS) {
1670 ql_dbg(ql_dbg_taskm, vha, 0x802c,
1671 "Bus Reset failed: Reset=%d "
1672 "d_id=%x.\n", ret, fcport->d_id.b24);
1673 }
1674 }
1675 }
1676
1677
1678 if (ha->flags.enable_lip_full_login && !IS_CNA_CAPABLE(ha)) {
1679 atomic_set(&vha->loop_state, LOOP_DOWN);
1680 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1681 qla2x00_mark_all_devices_lost(vha, 0);
1682 ret = qla2x00_full_login_lip(vha);
1683 if (ret != QLA_SUCCESS) {
1684 ql_dbg(ql_dbg_taskm, vha, 0x802d,
1685 "full_login_lip=%d.\n", ret);
1686 }
1687 }
1688
1689 if (ha->flags.enable_lip_reset) {
1690 ret = qla2x00_lip_reset(vha);
1691 if (ret != QLA_SUCCESS)
1692 ql_dbg(ql_dbg_taskm, vha, 0x802e,
1693 "lip_reset failed (%d).\n", ret);
1694 }
1695
1696 /* Issue marker command only when we are going to start the I/O */
1697 vha->marker_needed = 1;
1698
1699 return QLA_SUCCESS;
1700 }
1701
1702 static void qla2x00_abort_srb(struct qla_qpair *qp, srb_t *sp, const int res,
1703 unsigned long *flags)
1704 __releases(qp->qp_lock_ptr)
1705 __acquires(qp->qp_lock_ptr)
1706 {
1707 DECLARE_COMPLETION_ONSTACK(comp);
1708 scsi_qla_host_t *vha = qp->vha;
1709 struct qla_hw_data *ha = vha->hw;
1710 int rval;
1711
1712 if (sp_get(sp))
1713 return;
1714
1715 if (sp->type == SRB_NVME_CMD || sp->type == SRB_NVME_LS ||
1716 (sp->type == SRB_SCSI_CMD && !ha->flags.eeh_busy &&
1717 !test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) &&
1718 !qla2x00_isp_reg_stat(ha))) {
1719 sp->comp = &comp;
1720 spin_unlock_irqrestore(qp->qp_lock_ptr, *flags);
1721 rval = ha->isp_ops->abort_command(sp);
1722
1723 switch (rval) {
1724 case QLA_SUCCESS:
1725 sp->done(sp, res);
1726 break;
1727 case QLA_FUNCTION_PARAMETER_ERROR:
1728 wait_for_completion(&comp);
1729 break;
1730 }
1731
1732 spin_lock_irqsave(qp->qp_lock_ptr, *flags);
1733 sp->comp = NULL;
1734 }
1735
1736 atomic_dec(&sp->ref_count);
1737 }
1738
1739 static void
1740 __qla2x00_abort_all_cmds(struct qla_qpair *qp, int res)
1741 {
1742 int cnt;
1743 unsigned long flags;
1744 srb_t *sp;
1745 scsi_qla_host_t *vha = qp->vha;
1746 struct qla_hw_data *ha = vha->hw;
1747 struct req_que *req;
1748 struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1749 struct qla_tgt_cmd *cmd;
1750
1751 if (!ha->req_q_map)
1752 return;
1753 spin_lock_irqsave(qp->qp_lock_ptr, flags);
1754 req = qp->req;
1755 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
1756 sp = req->outstanding_cmds[cnt];
1757 if (sp) {
1758 req->outstanding_cmds[cnt] = NULL;
1759 switch (sp->cmd_type) {
1760 case TYPE_SRB:
1761 qla2x00_abort_srb(qp, sp, res, &flags);
1762 break;
1763 case TYPE_TGT_CMD:
1764 if (!vha->hw->tgt.tgt_ops || !tgt ||
1765 qla_ini_mode_enabled(vha)) {
1766 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf003,
1767 "HOST-ABORT-HNDLR: dpc_flags=%lx. Target mode disabled\n",
1768 vha->dpc_flags);
1769 continue;
1770 }
1771 cmd = (struct qla_tgt_cmd *)sp;
1772 cmd->aborted = 1;
1773 break;
1774 case TYPE_TGT_TMCMD:
1775 /* Skip task management functions. */
1776 break;
1777 default:
1778 break;
1779 }
1780 }
1781 }
1782 spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
1783 }
1784
1785 void
1786 qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
1787 {
1788 int que;
1789 struct qla_hw_data *ha = vha->hw;
1790
1791 /* Continue only if initialization complete. */
1792 if (!ha->base_qpair)
1793 return;
1794 __qla2x00_abort_all_cmds(ha->base_qpair, res);
1795
1796 if (!ha->queue_pair_map)
1797 return;
1798 for (que = 0; que < ha->max_qpairs; que++) {
1799 if (!ha->queue_pair_map[que])
1800 continue;
1801
1802 __qla2x00_abort_all_cmds(ha->queue_pair_map[que], res);
1803 }
1804 }
1805
1806 static int
1807 qla2xxx_slave_alloc(struct scsi_device *sdev)
1808 {
1809 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1810
1811 if (!rport || fc_remote_port_chkready(rport))
1812 return -ENXIO;
1813
1814 sdev->hostdata = *(fc_port_t **)rport->dd_data;
1815
1816 return 0;
1817 }
1818
1819 static int
1820 qla2xxx_slave_configure(struct scsi_device *sdev)
1821 {
1822 scsi_qla_host_t *vha = shost_priv(sdev->host);
1823 struct req_que *req = vha->req;
1824
1825 if (IS_T10_PI_CAPABLE(vha->hw))
1826 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1827
1828 scsi_change_queue_depth(sdev, req->max_q_depth);
1829 return 0;
1830 }
1831
1832 static void
1833 qla2xxx_slave_destroy(struct scsi_device *sdev)
1834 {
1835 sdev->hostdata = NULL;
1836 }
1837
1838 /**
1839 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1840 * @ha: HA context
1841 *
1842 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1843 * supported addressing method.
1844 */
1845 static void
1846 qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1847 {
1848 /* Assume a 32bit DMA mask. */
1849 ha->flags.enable_64bit_addressing = 0;
1850
1851 if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1852 /* Any upper-dword bits set? */
1853 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1854 !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1855 /* Ok, a 64bit DMA mask is applicable. */
1856 ha->flags.enable_64bit_addressing = 1;
1857 ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1858 ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1859 return;
1860 }
1861 }
1862
1863 dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1864 pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1865 }
1866
1867 static void
1868 qla2x00_enable_intrs(struct qla_hw_data *ha)
1869 {
1870 unsigned long flags = 0;
1871 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1872
1873 spin_lock_irqsave(&ha->hardware_lock, flags);
1874 ha->interrupts_on = 1;
1875 /* enable risc and host interrupts */
1876 WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1877 RD_REG_WORD(&reg->ictrl);
1878 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1879
1880 }
1881
1882 static void
1883 qla2x00_disable_intrs(struct qla_hw_data *ha)
1884 {
1885 unsigned long flags = 0;
1886 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1887
1888 spin_lock_irqsave(&ha->hardware_lock, flags);
1889 ha->interrupts_on = 0;
1890 /* disable risc and host interrupts */
1891 WRT_REG_WORD(&reg->ictrl, 0);
1892 RD_REG_WORD(&reg->ictrl);
1893 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1894 }
1895
1896 static void
1897 qla24xx_enable_intrs(struct qla_hw_data *ha)
1898 {
1899 unsigned long flags = 0;
1900 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1901
1902 spin_lock_irqsave(&ha->hardware_lock, flags);
1903 ha->interrupts_on = 1;
1904 WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1905 RD_REG_DWORD(&reg->ictrl);
1906 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1907 }
1908
1909 static void
1910 qla24xx_disable_intrs(struct qla_hw_data *ha)
1911 {
1912 unsigned long flags = 0;
1913 struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1914
1915 if (IS_NOPOLLING_TYPE(ha))
1916 return;
1917 spin_lock_irqsave(&ha->hardware_lock, flags);
1918 ha->interrupts_on = 0;
1919 WRT_REG_DWORD(&reg->ictrl, 0);
1920 RD_REG_DWORD(&reg->ictrl);
1921 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1922 }
1923
1924 static int
1925 qla2x00_iospace_config(struct qla_hw_data *ha)
1926 {
1927 resource_size_t pio;
1928 uint16_t msix;
1929
1930 if (pci_request_selected_regions(ha->pdev, ha->bars,
1931 QLA2XXX_DRIVER_NAME)) {
1932 ql_log_pci(ql_log_fatal, ha->pdev, 0x0011,
1933 "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
1934 pci_name(ha->pdev));
1935 goto iospace_error_exit;
1936 }
1937 if (!(ha->bars & 1))
1938 goto skip_pio;
1939
1940 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1941 pio = pci_resource_start(ha->pdev, 0);
1942 if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1943 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1944 ql_log_pci(ql_log_warn, ha->pdev, 0x0012,
1945 "Invalid pci I/O region size (%s).\n",
1946 pci_name(ha->pdev));
1947 pio = 0;
1948 }
1949 } else {
1950 ql_log_pci(ql_log_warn, ha->pdev, 0x0013,
1951 "Region #0 no a PIO resource (%s).\n",
1952 pci_name(ha->pdev));
1953 pio = 0;
1954 }
1955 ha->pio_address = pio;
1956 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0014,
1957 "PIO address=%llu.\n",
1958 (unsigned long long)ha->pio_address);
1959
1960 skip_pio:
1961 /* Use MMIO operations for all accesses. */
1962 if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
1963 ql_log_pci(ql_log_fatal, ha->pdev, 0x0015,
1964 "Region #1 not an MMIO resource (%s), aborting.\n",
1965 pci_name(ha->pdev));
1966 goto iospace_error_exit;
1967 }
1968 if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
1969 ql_log_pci(ql_log_fatal, ha->pdev, 0x0016,
1970 "Invalid PCI mem region size (%s), aborting.\n",
1971 pci_name(ha->pdev));
1972 goto iospace_error_exit;
1973 }
1974
1975 ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
1976 if (!ha->iobase) {
1977 ql_log_pci(ql_log_fatal, ha->pdev, 0x0017,
1978 "Cannot remap MMIO (%s), aborting.\n",
1979 pci_name(ha->pdev));
1980 goto iospace_error_exit;
1981 }
1982
1983 /* Determine queue resources */
1984 ha->max_req_queues = ha->max_rsp_queues = 1;
1985 ha->msix_count = QLA_BASE_VECTORS;
1986 if (!ql2xmqsupport || !ql2xnvmeenable ||
1987 (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
1988 goto mqiobase_exit;
1989
1990 ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1991 pci_resource_len(ha->pdev, 3));
1992 if (ha->mqiobase) {
1993 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0018,
1994 "MQIO Base=%p.\n", ha->mqiobase);
1995 /* Read MSIX vector size of the board */
1996 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1997 ha->msix_count = msix + 1;
1998 /* Max queues are bounded by available msix vectors */
1999 /* MB interrupt uses 1 vector */
2000 ha->max_req_queues = ha->msix_count - 1;
2001 ha->max_rsp_queues = ha->max_req_queues;
2002 /* Queue pairs is the max value minus the base queue pair */
2003 ha->max_qpairs = ha->max_rsp_queues - 1;
2004 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0188,
2005 "Max no of queues pairs: %d.\n", ha->max_qpairs);
2006
2007 ql_log_pci(ql_log_info, ha->pdev, 0x001a,
2008 "MSI-X vector count: %d.\n", ha->msix_count);
2009 } else
2010 ql_log_pci(ql_log_info, ha->pdev, 0x001b,
2011 "BAR 3 not enabled.\n");
2012
2013 mqiobase_exit:
2014 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x001c,
2015 "MSIX Count: %d.\n", ha->msix_count);
2016 return (0);
2017
2018 iospace_error_exit:
2019 return (-ENOMEM);
2020 }
2021
2022
2023 static int
2024 qla83xx_iospace_config(struct qla_hw_data *ha)
2025 {
2026 uint16_t msix;
2027
2028 if (pci_request_selected_regions(ha->pdev, ha->bars,
2029 QLA2XXX_DRIVER_NAME)) {
2030 ql_log_pci(ql_log_fatal, ha->pdev, 0x0117,
2031 "Failed to reserve PIO/MMIO regions (%s), aborting.\n",
2032 pci_name(ha->pdev));
2033
2034 goto iospace_error_exit;
2035 }
2036
2037 /* Use MMIO operations for all accesses. */
2038 if (!(pci_resource_flags(ha->pdev, 0) & IORESOURCE_MEM)) {
2039 ql_log_pci(ql_log_warn, ha->pdev, 0x0118,
2040 "Invalid pci I/O region size (%s).\n",
2041 pci_name(ha->pdev));
2042 goto iospace_error_exit;
2043 }
2044 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
2045 ql_log_pci(ql_log_warn, ha->pdev, 0x0119,
2046 "Invalid PCI mem region size (%s), aborting\n",
2047 pci_name(ha->pdev));
2048 goto iospace_error_exit;
2049 }
2050
2051 ha->iobase = ioremap(pci_resource_start(ha->pdev, 0), MIN_IOBASE_LEN);
2052 if (!ha->iobase) {
2053 ql_log_pci(ql_log_fatal, ha->pdev, 0x011a,
2054 "Cannot remap MMIO (%s), aborting.\n",
2055 pci_name(ha->pdev));
2056 goto iospace_error_exit;
2057 }
2058
2059 /* 64bit PCI BAR - BAR2 will correspoond to region 4 */
2060 /* 83XX 26XX always use MQ type access for queues
2061 * - mbar 2, a.k.a region 4 */
2062 ha->max_req_queues = ha->max_rsp_queues = 1;
2063 ha->msix_count = QLA_BASE_VECTORS;
2064 ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 4),
2065 pci_resource_len(ha->pdev, 4));
2066
2067 if (!ha->mqiobase) {
2068 ql_log_pci(ql_log_fatal, ha->pdev, 0x011d,
2069 "BAR2/region4 not enabled\n");
2070 goto mqiobase_exit;
2071 }
2072
2073 ha->msixbase = ioremap(pci_resource_start(ha->pdev, 2),
2074 pci_resource_len(ha->pdev, 2));
2075 if (ha->msixbase) {
2076 /* Read MSIX vector size of the board */
2077 pci_read_config_word(ha->pdev,
2078 QLA_83XX_PCI_MSIX_CONTROL, &msix);
2079 ha->msix_count = (msix & PCI_MSIX_FLAGS_QSIZE) + 1;
2080 /*
2081 * By default, driver uses at least two msix vectors
2082 * (default & rspq)
2083 */
2084 if (ql2xmqsupport || ql2xnvmeenable) {
2085 /* MB interrupt uses 1 vector */
2086 ha->max_req_queues = ha->msix_count - 1;
2087
2088 /* ATIOQ needs 1 vector. That's 1 less QPair */
2089 if (QLA_TGT_MODE_ENABLED())
2090 ha->max_req_queues--;
2091
2092 ha->max_rsp_queues = ha->max_req_queues;
2093
2094 /* Queue pairs is the max value minus
2095 * the base queue pair */
2096 ha->max_qpairs = ha->max_req_queues - 1;
2097 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x00e3,
2098 "Max no of queues pairs: %d.\n", ha->max_qpairs);
2099 }
2100 ql_log_pci(ql_log_info, ha->pdev, 0x011c,
2101 "MSI-X vector count: %d.\n", ha->msix_count);
2102 } else
2103 ql_log_pci(ql_log_info, ha->pdev, 0x011e,
2104 "BAR 1 not enabled.\n");
2105
2106 mqiobase_exit:
2107 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011f,
2108 "MSIX Count: %d.\n", ha->msix_count);
2109 return 0;
2110
2111 iospace_error_exit:
2112 return -ENOMEM;
2113 }
2114
2115 static struct isp_operations qla2100_isp_ops = {
2116 .pci_config = qla2100_pci_config,
2117 .reset_chip = qla2x00_reset_chip,
2118 .chip_diag = qla2x00_chip_diag,
2119 .config_rings = qla2x00_config_rings,
2120 .reset_adapter = qla2x00_reset_adapter,
2121 .nvram_config = qla2x00_nvram_config,
2122 .update_fw_options = qla2x00_update_fw_options,
2123 .load_risc = qla2x00_load_risc,
2124 .pci_info_str = qla2x00_pci_info_str,
2125 .fw_version_str = qla2x00_fw_version_str,
2126 .intr_handler = qla2100_intr_handler,
2127 .enable_intrs = qla2x00_enable_intrs,
2128 .disable_intrs = qla2x00_disable_intrs,
2129 .abort_command = qla2x00_abort_command,
2130 .target_reset = qla2x00_abort_target,
2131 .lun_reset = qla2x00_lun_reset,
2132 .fabric_login = qla2x00_login_fabric,
2133 .fabric_logout = qla2x00_fabric_logout,
2134 .calc_req_entries = qla2x00_calc_iocbs_32,
2135 .build_iocbs = qla2x00_build_scsi_iocbs_32,
2136 .prep_ms_iocb = qla2x00_prep_ms_iocb,
2137 .prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb,
2138 .read_nvram = qla2x00_read_nvram_data,
2139 .write_nvram = qla2x00_write_nvram_data,
2140 .fw_dump = qla2100_fw_dump,
2141 .beacon_on = NULL,
2142 .beacon_off = NULL,
2143 .beacon_blink = NULL,
2144 .read_optrom = qla2x00_read_optrom_data,
2145 .write_optrom = qla2x00_write_optrom_data,
2146 .get_flash_version = qla2x00_get_flash_version,
2147 .start_scsi = qla2x00_start_scsi,
2148 .start_scsi_mq = NULL,
2149 .abort_isp = qla2x00_abort_isp,
2150 .iospace_config = qla2x00_iospace_config,
2151 .initialize_adapter = qla2x00_initialize_adapter,
2152 };
2153
2154 static struct isp_operations qla2300_isp_ops = {
2155 .pci_config = qla2300_pci_config,
2156 .reset_chip = qla2x00_reset_chip,
2157 .chip_diag = qla2x00_chip_diag,
2158 .config_rings = qla2x00_config_rings,
2159 .reset_adapter = qla2x00_reset_adapter,
2160 .nvram_config = qla2x00_nvram_config,
2161 .update_fw_options = qla2x00_update_fw_options,
2162 .load_risc = qla2x00_load_risc,
2163 .pci_info_str = qla2x00_pci_info_str,
2164 .fw_version_str = qla2x00_fw_version_str,
2165 .intr_handler = qla2300_intr_handler,
2166 .enable_intrs = qla2x00_enable_intrs,
2167 .disable_intrs = qla2x00_disable_intrs,
2168 .abort_command = qla2x00_abort_command,
2169 .target_reset = qla2x00_abort_target,
2170 .lun_reset = qla2x00_lun_reset,
2171 .fabric_login = qla2x00_login_fabric,
2172 .fabric_logout = qla2x00_fabric_logout,
2173 .calc_req_entries = qla2x00_calc_iocbs_32,
2174 .build_iocbs = qla2x00_build_scsi_iocbs_32,
2175 .prep_ms_iocb = qla2x00_prep_ms_iocb,
2176 .prep_ms_fdmi_iocb = qla2x00_prep_ms_fdmi_iocb,
2177 .read_nvram = qla2x00_read_nvram_data,
2178 .write_nvram = qla2x00_write_nvram_data,
2179 .fw_dump = qla2300_fw_dump,
2180 .beacon_on = qla2x00_beacon_on,
2181 .beacon_off = qla2x00_beacon_off,
2182 .beacon_blink = qla2x00_beacon_blink,
2183 .read_optrom = qla2x00_read_optrom_data,
2184 .write_optrom = qla2x00_write_optrom_data,
2185 .get_flash_version = qla2x00_get_flash_version,
2186 .start_scsi = qla2x00_start_scsi,
2187 .start_scsi_mq = NULL,
2188 .abort_isp = qla2x00_abort_isp,
2189 .iospace_config = qla2x00_iospace_config,
2190 .initialize_adapter = qla2x00_initialize_adapter,
2191 };
2192
2193 static struct isp_operations qla24xx_isp_ops = {
2194 .pci_config = qla24xx_pci_config,
2195 .reset_chip = qla24xx_reset_chip,
2196 .chip_diag = qla24xx_chip_diag,
2197 .config_rings = qla24xx_config_rings,
2198 .reset_adapter = qla24xx_reset_adapter,
2199 .nvram_config = qla24xx_nvram_config,
2200 .update_fw_options = qla24xx_update_fw_options,
2201 .load_risc = qla24xx_load_risc,
2202 .pci_info_str = qla24xx_pci_info_str,
2203 .fw_version_str = qla24xx_fw_version_str,
2204 .intr_handler = qla24xx_intr_handler,
2205 .enable_intrs = qla24xx_enable_intrs,
2206 .disable_intrs = qla24xx_disable_intrs,
2207 .abort_command = qla24xx_abort_command,
2208 .target_reset = qla24xx_abort_target,
2209 .lun_reset = qla24xx_lun_reset,
2210 .fabric_login = qla24xx_login_fabric,
2211 .fabric_logout = qla24xx_fabric_logout,
2212 .calc_req_entries = NULL,
2213 .build_iocbs = NULL,
2214 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2215 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2216 .read_nvram = qla24xx_read_nvram_data,
2217 .write_nvram = qla24xx_write_nvram_data,
2218 .fw_dump = qla24xx_fw_dump,
2219 .beacon_on = qla24xx_beacon_on,
2220 .beacon_off = qla24xx_beacon_off,
2221 .beacon_blink = qla24xx_beacon_blink,
2222 .read_optrom = qla24xx_read_optrom_data,
2223 .write_optrom = qla24xx_write_optrom_data,
2224 .get_flash_version = qla24xx_get_flash_version,
2225 .start_scsi = qla24xx_start_scsi,
2226 .start_scsi_mq = NULL,
2227 .abort_isp = qla2x00_abort_isp,
2228 .iospace_config = qla2x00_iospace_config,
2229 .initialize_adapter = qla2x00_initialize_adapter,
2230 };
2231
2232 static struct isp_operations qla25xx_isp_ops = {
2233 .pci_config = qla25xx_pci_config,
2234 .reset_chip = qla24xx_reset_chip,
2235 .chip_diag = qla24xx_chip_diag,
2236 .config_rings = qla24xx_config_rings,
2237 .reset_adapter = qla24xx_reset_adapter,
2238 .nvram_config = qla24xx_nvram_config,
2239 .update_fw_options = qla24xx_update_fw_options,
2240 .load_risc = qla24xx_load_risc,
2241 .pci_info_str = qla24xx_pci_info_str,
2242 .fw_version_str = qla24xx_fw_version_str,
2243 .intr_handler = qla24xx_intr_handler,
2244 .enable_intrs = qla24xx_enable_intrs,
2245 .disable_intrs = qla24xx_disable_intrs,
2246 .abort_command = qla24xx_abort_command,
2247 .target_reset = qla24xx_abort_target,
2248 .lun_reset = qla24xx_lun_reset,
2249 .fabric_login = qla24xx_login_fabric,
2250 .fabric_logout = qla24xx_fabric_logout,
2251 .calc_req_entries = NULL,
2252 .build_iocbs = NULL,
2253 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2254 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2255 .read_nvram = qla25xx_read_nvram_data,
2256 .write_nvram = qla25xx_write_nvram_data,
2257 .fw_dump = qla25xx_fw_dump,
2258 .beacon_on = qla24xx_beacon_on,
2259 .beacon_off = qla24xx_beacon_off,
2260 .beacon_blink = qla24xx_beacon_blink,
2261 .read_optrom = qla25xx_read_optrom_data,
2262 .write_optrom = qla24xx_write_optrom_data,
2263 .get_flash_version = qla24xx_get_flash_version,
2264 .start_scsi = qla24xx_dif_start_scsi,
2265 .start_scsi_mq = qla2xxx_dif_start_scsi_mq,
2266 .abort_isp = qla2x00_abort_isp,
2267 .iospace_config = qla2x00_iospace_config,
2268 .initialize_adapter = qla2x00_initialize_adapter,
2269 };
2270
2271 static struct isp_operations qla81xx_isp_ops = {
2272 .pci_config = qla25xx_pci_config,
2273 .reset_chip = qla24xx_reset_chip,
2274 .chip_diag = qla24xx_chip_diag,
2275 .config_rings = qla24xx_config_rings,
2276 .reset_adapter = qla24xx_reset_adapter,
2277 .nvram_config = qla81xx_nvram_config,
2278 .update_fw_options = qla81xx_update_fw_options,
2279 .load_risc = qla81xx_load_risc,
2280 .pci_info_str = qla24xx_pci_info_str,
2281 .fw_version_str = qla24xx_fw_version_str,
2282 .intr_handler = qla24xx_intr_handler,
2283 .enable_intrs = qla24xx_enable_intrs,
2284 .disable_intrs = qla24xx_disable_intrs,
2285 .abort_command = qla24xx_abort_command,
2286 .target_reset = qla24xx_abort_target,
2287 .lun_reset = qla24xx_lun_reset,
2288 .fabric_login = qla24xx_login_fabric,
2289 .fabric_logout = qla24xx_fabric_logout,
2290 .calc_req_entries = NULL,
2291 .build_iocbs = NULL,
2292 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2293 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2294 .read_nvram = NULL,
2295 .write_nvram = NULL,
2296 .fw_dump = qla81xx_fw_dump,
2297 .beacon_on = qla24xx_beacon_on,
2298 .beacon_off = qla24xx_beacon_off,
2299 .beacon_blink = qla83xx_beacon_blink,
2300 .read_optrom = qla25xx_read_optrom_data,
2301 .write_optrom = qla24xx_write_optrom_data,
2302 .get_flash_version = qla24xx_get_flash_version,
2303 .start_scsi = qla24xx_dif_start_scsi,
2304 .start_scsi_mq = qla2xxx_dif_start_scsi_mq,
2305 .abort_isp = qla2x00_abort_isp,
2306 .iospace_config = qla2x00_iospace_config,
2307 .initialize_adapter = qla2x00_initialize_adapter,
2308 };
2309
2310 static struct isp_operations qla82xx_isp_ops = {
2311 .pci_config = qla82xx_pci_config,
2312 .reset_chip = qla82xx_reset_chip,
2313 .chip_diag = qla24xx_chip_diag,
2314 .config_rings = qla82xx_config_rings,
2315 .reset_adapter = qla24xx_reset_adapter,
2316 .nvram_config = qla81xx_nvram_config,
2317 .update_fw_options = qla24xx_update_fw_options,
2318 .load_risc = qla82xx_load_risc,
2319 .pci_info_str = qla24xx_pci_info_str,
2320 .fw_version_str = qla24xx_fw_version_str,
2321 .intr_handler = qla82xx_intr_handler,
2322 .enable_intrs = qla82xx_enable_intrs,
2323 .disable_intrs = qla82xx_disable_intrs,
2324 .abort_command = qla24xx_abort_command,
2325 .target_reset = qla24xx_abort_target,
2326 .lun_reset = qla24xx_lun_reset,
2327 .fabric_login = qla24xx_login_fabric,
2328 .fabric_logout = qla24xx_fabric_logout,
2329 .calc_req_entries = NULL,
2330 .build_iocbs = NULL,
2331 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2332 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2333 .read_nvram = qla24xx_read_nvram_data,
2334 .write_nvram = qla24xx_write_nvram_data,
2335 .fw_dump = qla82xx_fw_dump,
2336 .beacon_on = qla82xx_beacon_on,
2337 .beacon_off = qla82xx_beacon_off,
2338 .beacon_blink = NULL,
2339 .read_optrom = qla82xx_read_optrom_data,
2340 .write_optrom = qla82xx_write_optrom_data,
2341 .get_flash_version = qla82xx_get_flash_version,
2342 .start_scsi = qla82xx_start_scsi,
2343 .start_scsi_mq = NULL,
2344 .abort_isp = qla82xx_abort_isp,
2345 .iospace_config = qla82xx_iospace_config,
2346 .initialize_adapter = qla2x00_initialize_adapter,
2347 };
2348
2349 static struct isp_operations qla8044_isp_ops = {
2350 .pci_config = qla82xx_pci_config,
2351 .reset_chip = qla82xx_reset_chip,
2352 .chip_diag = qla24xx_chip_diag,
2353 .config_rings = qla82xx_config_rings,
2354 .reset_adapter = qla24xx_reset_adapter,
2355 .nvram_config = qla81xx_nvram_config,
2356 .update_fw_options = qla24xx_update_fw_options,
2357 .load_risc = qla82xx_load_risc,
2358 .pci_info_str = qla24xx_pci_info_str,
2359 .fw_version_str = qla24xx_fw_version_str,
2360 .intr_handler = qla8044_intr_handler,
2361 .enable_intrs = qla82xx_enable_intrs,
2362 .disable_intrs = qla82xx_disable_intrs,
2363 .abort_command = qla24xx_abort_command,
2364 .target_reset = qla24xx_abort_target,
2365 .lun_reset = qla24xx_lun_reset,
2366 .fabric_login = qla24xx_login_fabric,
2367 .fabric_logout = qla24xx_fabric_logout,
2368 .calc_req_entries = NULL,
2369 .build_iocbs = NULL,
2370 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2371 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2372 .read_nvram = NULL,
2373 .write_nvram = NULL,
2374 .fw_dump = qla8044_fw_dump,
2375 .beacon_on = qla82xx_beacon_on,
2376 .beacon_off = qla82xx_beacon_off,
2377 .beacon_blink = NULL,
2378 .read_optrom = qla8044_read_optrom_data,
2379 .write_optrom = qla8044_write_optrom_data,
2380 .get_flash_version = qla82xx_get_flash_version,
2381 .start_scsi = qla82xx_start_scsi,
2382 .start_scsi_mq = NULL,
2383 .abort_isp = qla8044_abort_isp,
2384 .iospace_config = qla82xx_iospace_config,
2385 .initialize_adapter = qla2x00_initialize_adapter,
2386 };
2387
2388 static struct isp_operations qla83xx_isp_ops = {
2389 .pci_config = qla25xx_pci_config,
2390 .reset_chip = qla24xx_reset_chip,
2391 .chip_diag = qla24xx_chip_diag,
2392 .config_rings = qla24xx_config_rings,
2393 .reset_adapter = qla24xx_reset_adapter,
2394 .nvram_config = qla81xx_nvram_config,
2395 .update_fw_options = qla81xx_update_fw_options,
2396 .load_risc = qla81xx_load_risc,
2397 .pci_info_str = qla24xx_pci_info_str,
2398 .fw_version_str = qla24xx_fw_version_str,
2399 .intr_handler = qla24xx_intr_handler,
2400 .enable_intrs = qla24xx_enable_intrs,
2401 .disable_intrs = qla24xx_disable_intrs,
2402 .abort_command = qla24xx_abort_command,
2403 .target_reset = qla24xx_abort_target,
2404 .lun_reset = qla24xx_lun_reset,
2405 .fabric_login = qla24xx_login_fabric,
2406 .fabric_logout = qla24xx_fabric_logout,
2407 .calc_req_entries = NULL,
2408 .build_iocbs = NULL,
2409 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2410 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2411 .read_nvram = NULL,
2412 .write_nvram = NULL,
2413 .fw_dump = qla83xx_fw_dump,
2414 .beacon_on = qla24xx_beacon_on,
2415 .beacon_off = qla24xx_beacon_off,
2416 .beacon_blink = qla83xx_beacon_blink,
2417 .read_optrom = qla25xx_read_optrom_data,
2418 .write_optrom = qla24xx_write_optrom_data,
2419 .get_flash_version = qla24xx_get_flash_version,
2420 .start_scsi = qla24xx_dif_start_scsi,
2421 .start_scsi_mq = qla2xxx_dif_start_scsi_mq,
2422 .abort_isp = qla2x00_abort_isp,
2423 .iospace_config = qla83xx_iospace_config,
2424 .initialize_adapter = qla2x00_initialize_adapter,
2425 };
2426
2427 static struct isp_operations qlafx00_isp_ops = {
2428 .pci_config = qlafx00_pci_config,
2429 .reset_chip = qlafx00_soft_reset,
2430 .chip_diag = qlafx00_chip_diag,
2431 .config_rings = qlafx00_config_rings,
2432 .reset_adapter = qlafx00_soft_reset,
2433 .nvram_config = NULL,
2434 .update_fw_options = NULL,
2435 .load_risc = NULL,
2436 .pci_info_str = qlafx00_pci_info_str,
2437 .fw_version_str = qlafx00_fw_version_str,
2438 .intr_handler = qlafx00_intr_handler,
2439 .enable_intrs = qlafx00_enable_intrs,
2440 .disable_intrs = qlafx00_disable_intrs,
2441 .abort_command = qla24xx_async_abort_command,
2442 .target_reset = qlafx00_abort_target,
2443 .lun_reset = qlafx00_lun_reset,
2444 .fabric_login = NULL,
2445 .fabric_logout = NULL,
2446 .calc_req_entries = NULL,
2447 .build_iocbs = NULL,
2448 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2449 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2450 .read_nvram = qla24xx_read_nvram_data,
2451 .write_nvram = qla24xx_write_nvram_data,
2452 .fw_dump = NULL,
2453 .beacon_on = qla24xx_beacon_on,
2454 .beacon_off = qla24xx_beacon_off,
2455 .beacon_blink = NULL,
2456 .read_optrom = qla24xx_read_optrom_data,
2457 .write_optrom = qla24xx_write_optrom_data,
2458 .get_flash_version = qla24xx_get_flash_version,
2459 .start_scsi = qlafx00_start_scsi,
2460 .start_scsi_mq = NULL,
2461 .abort_isp = qlafx00_abort_isp,
2462 .iospace_config = qlafx00_iospace_config,
2463 .initialize_adapter = qlafx00_initialize_adapter,
2464 };
2465
2466 static struct isp_operations qla27xx_isp_ops = {
2467 .pci_config = qla25xx_pci_config,
2468 .reset_chip = qla24xx_reset_chip,
2469 .chip_diag = qla24xx_chip_diag,
2470 .config_rings = qla24xx_config_rings,
2471 .reset_adapter = qla24xx_reset_adapter,
2472 .nvram_config = qla81xx_nvram_config,
2473 .update_fw_options = qla24xx_update_fw_options,
2474 .load_risc = qla81xx_load_risc,
2475 .pci_info_str = qla24xx_pci_info_str,
2476 .fw_version_str = qla24xx_fw_version_str,
2477 .intr_handler = qla24xx_intr_handler,
2478 .enable_intrs = qla24xx_enable_intrs,
2479 .disable_intrs = qla24xx_disable_intrs,
2480 .abort_command = qla24xx_abort_command,
2481 .target_reset = qla24xx_abort_target,
2482 .lun_reset = qla24xx_lun_reset,
2483 .fabric_login = qla24xx_login_fabric,
2484 .fabric_logout = qla24xx_fabric_logout,
2485 .calc_req_entries = NULL,
2486 .build_iocbs = NULL,
2487 .prep_ms_iocb = qla24xx_prep_ms_iocb,
2488 .prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb,
2489 .read_nvram = NULL,
2490 .write_nvram = NULL,
2491 .fw_dump = qla27xx_fwdump,
2492 .beacon_on = qla24xx_beacon_on,
2493 .beacon_off = qla24xx_beacon_off,
2494 .beacon_blink = qla83xx_beacon_blink,
2495 .read_optrom = qla25xx_read_optrom_data,
2496 .write_optrom = qla24xx_write_optrom_data,
2497 .get_flash_version = qla24xx_get_flash_version,
2498 .start_scsi = qla24xx_dif_start_scsi,
2499 .start_scsi_mq = qla2xxx_dif_start_scsi_mq,
2500 .abort_isp = qla2x00_abort_isp,
2501 .iospace_config = qla83xx_iospace_config,
2502 .initialize_adapter = qla2x00_initialize_adapter,
2503 };
2504
2505 static inline void
2506 qla2x00_set_isp_flags(struct qla_hw_data *ha)
2507 {
2508 ha->device_type = DT_EXTENDED_IDS;
2509 switch (ha->pdev->device) {
2510 case PCI_DEVICE_ID_QLOGIC_ISP2100:
2511 ha->isp_type |= DT_ISP2100;
2512 ha->device_type &= ~DT_EXTENDED_IDS;
2513 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
2514 break;
2515 case PCI_DEVICE_ID_QLOGIC_ISP2200:
2516 ha->isp_type |= DT_ISP2200;
2517 ha->device_type &= ~DT_EXTENDED_IDS;
2518 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
2519 break;
2520 case PCI_DEVICE_ID_QLOGIC_ISP2300:
2521 ha->isp_type |= DT_ISP2300;
2522 ha->device_type |= DT_ZIO_SUPPORTED;
2523 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2524 break;
2525 case PCI_DEVICE_ID_QLOGIC_ISP2312:
2526 ha->isp_type |= DT_ISP2312;
2527 ha->device_type |= DT_ZIO_SUPPORTED;
2528 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2529 break;
2530 case PCI_DEVICE_ID_QLOGIC_ISP2322:
2531 ha->isp_type |= DT_ISP2322;
2532 ha->device_type |= DT_ZIO_SUPPORTED;
2533 if (ha->pdev->subsystem_vendor == 0x1028 &&
2534 ha->pdev->subsystem_device == 0x0170)
2535 ha->device_type |= DT_OEM_001;
2536 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2537 break;
2538 case PCI_DEVICE_ID_QLOGIC_ISP6312:
2539 ha->isp_type |= DT_ISP6312;
2540 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2541 break;
2542 case PCI_DEVICE_ID_QLOGIC_ISP6322:
2543 ha->isp_type |= DT_ISP6322;
2544 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
2545 break;
2546 case PCI_DEVICE_ID_QLOGIC_ISP2422:
2547 ha->isp_type |= DT_ISP2422;
2548 ha->device_type |= DT_ZIO_SUPPORTED;
2549 ha->device_type |= DT_FWI2;
2550 ha->device_type |= DT_IIDMA;
2551 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2552 break;
2553 case PCI_DEVICE_ID_QLOGIC_ISP2432:
2554 ha->isp_type |= DT_ISP2432;
2555 ha->device_type |= DT_ZIO_SUPPORTED;
2556 ha->device_type |= DT_FWI2;
2557 ha->device_type |= DT_IIDMA;
2558 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2559 break;
2560 case PCI_DEVICE_ID_QLOGIC_ISP8432:
2561 ha->isp_type |= DT_ISP8432;
2562 ha->device_type |= DT_ZIO_SUPPORTED;
2563 ha->device_type |= DT_FWI2;
2564 ha->device_type |= DT_IIDMA;
2565 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2566 break;
2567 case PCI_DEVICE_ID_QLOGIC_ISP5422:
2568 ha->isp_type |= DT_ISP5422;
2569 ha->device_type |= DT_FWI2;
2570 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2571 break;
2572 case PCI_DEVICE_ID_QLOGIC_ISP5432:
2573 ha->isp_type |= DT_ISP5432;
2574 ha->device_type |= DT_FWI2;
2575 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2576 break;
2577 case PCI_DEVICE_ID_QLOGIC_ISP2532:
2578 ha->isp_type |= DT_ISP2532;
2579 ha->device_type |= DT_ZIO_SUPPORTED;
2580 ha->device_type |= DT_FWI2;
2581 ha->device_type |= DT_IIDMA;
2582 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2583 break;
2584 case PCI_DEVICE_ID_QLOGIC_ISP8001:
2585 ha->isp_type |= DT_ISP8001;
2586 ha->device_type |= DT_ZIO_SUPPORTED;
2587 ha->device_type |= DT_FWI2;
2588 ha->device_type |= DT_IIDMA;
2589 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2590 break;
2591 case PCI_DEVICE_ID_QLOGIC_ISP8021:
2592 ha->isp_type |= DT_ISP8021;
2593 ha->device_type |= DT_ZIO_SUPPORTED;
2594 ha->device_type |= DT_FWI2;
2595 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2596 /* Initialize 82XX ISP flags */
2597 qla82xx_init_flags(ha);
2598 break;
2599 case PCI_DEVICE_ID_QLOGIC_ISP8044:
2600 ha->isp_type |= DT_ISP8044;
2601 ha->device_type |= DT_ZIO_SUPPORTED;
2602 ha->device_type |= DT_FWI2;
2603 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2604 /* Initialize 82XX ISP flags */
2605 qla82xx_init_flags(ha);
2606 break;
2607 case PCI_DEVICE_ID_QLOGIC_ISP2031:
2608 ha->isp_type |= DT_ISP2031;
2609 ha->device_type |= DT_ZIO_SUPPORTED;
2610 ha->device_type |= DT_FWI2;
2611 ha->device_type |= DT_IIDMA;
2612 ha->device_type |= DT_T10_PI;
2613 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2614 break;
2615 case PCI_DEVICE_ID_QLOGIC_ISP8031:
2616 ha->isp_type |= DT_ISP8031;
2617 ha->device_type |= DT_ZIO_SUPPORTED;
2618 ha->device_type |= DT_FWI2;
2619 ha->device_type |= DT_IIDMA;
2620 ha->device_type |= DT_T10_PI;
2621 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2622 break;
2623 case PCI_DEVICE_ID_QLOGIC_ISPF001:
2624 ha->isp_type |= DT_ISPFX00;
2625 break;
2626 case PCI_DEVICE_ID_QLOGIC_ISP2071:
2627 ha->isp_type |= DT_ISP2071;
2628 ha->device_type |= DT_ZIO_SUPPORTED;
2629 ha->device_type |= DT_FWI2;
2630 ha->device_type |= DT_IIDMA;
2631 ha->device_type |= DT_T10_PI;
2632 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2633 break;
2634 case PCI_DEVICE_ID_QLOGIC_ISP2271:
2635 ha->isp_type |= DT_ISP2271;
2636 ha->device_type |= DT_ZIO_SUPPORTED;
2637 ha->device_type |= DT_FWI2;
2638 ha->device_type |= DT_IIDMA;
2639 ha->device_type |= DT_T10_PI;
2640 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2641 break;
2642 case PCI_DEVICE_ID_QLOGIC_ISP2261:
2643 ha->isp_type |= DT_ISP2261;
2644 ha->device_type |= DT_ZIO_SUPPORTED;
2645 ha->device_type |= DT_FWI2;
2646 ha->device_type |= DT_IIDMA;
2647 ha->device_type |= DT_T10_PI;
2648 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2649 break;
2650 case PCI_DEVICE_ID_QLOGIC_ISP2081:
2651 case PCI_DEVICE_ID_QLOGIC_ISP2089:
2652 ha->isp_type |= DT_ISP2081;
2653 ha->device_type |= DT_ZIO_SUPPORTED;
2654 ha->device_type |= DT_FWI2;
2655 ha->device_type |= DT_IIDMA;
2656 ha->device_type |= DT_T10_PI;
2657 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2658 break;
2659 case PCI_DEVICE_ID_QLOGIC_ISP2281:
2660 case PCI_DEVICE_ID_QLOGIC_ISP2289:
2661 ha->isp_type |= DT_ISP2281;
2662 ha->device_type |= DT_ZIO_SUPPORTED;
2663 ha->device_type |= DT_FWI2;
2664 ha->device_type |= DT_IIDMA;
2665 ha->device_type |= DT_T10_PI;
2666 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
2667 break;
2668 }
2669
2670 if (IS_QLA82XX(ha))
2671 ha->port_no = ha->portnum & 1;
2672 else {
2673 /* Get adapter physical port no from interrupt pin register. */
2674 pci_read_config_byte(ha->pdev, PCI_INTERRUPT_PIN, &ha->port_no);
2675 if (IS_QLA25XX(ha) || IS_QLA2031(ha) ||
2676 IS_QLA27XX(ha) || IS_QLA28XX(ha))
2677 ha->port_no--;
2678 else
2679 ha->port_no = !(ha->port_no & 1);
2680 }
2681
2682 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x000b,
2683 "device_type=0x%x port=%d fw_srisc_address=0x%x.\n",
2684 ha->device_type, ha->port_no, ha->fw_srisc_address);
2685 }
2686
2687 static void
2688 qla2xxx_scan_start(struct Scsi_Host *shost)
2689 {
2690 scsi_qla_host_t *vha = shost_priv(shost);
2691
2692 if (vha->hw->flags.running_gold_fw)
2693 return;
2694
2695 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
2696 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
2697 set_bit(RSCN_UPDATE, &vha->dpc_flags);
2698 set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
2699 }
2700
2701 static int
2702 qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
2703 {
2704 scsi_qla_host_t *vha = shost_priv(shost);
2705
2706 if (test_bit(UNLOADING, &vha->dpc_flags))
2707 return 1;
2708 if (!vha->host)
2709 return 1;
2710 if (time > vha->hw->loop_reset_delay * HZ)
2711 return 1;
2712
2713 return atomic_read(&vha->loop_state) == LOOP_READY;
2714 }
2715
2716 static void qla2x00_iocb_work_fn(struct work_struct *work)
2717 {
2718 struct scsi_qla_host *vha = container_of(work,
2719 struct scsi_qla_host, iocb_work);
2720 struct qla_hw_data *ha = vha->hw;
2721 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2722 int i = 2;
2723 unsigned long flags;
2724
2725 if (test_bit(UNLOADING, &base_vha->dpc_flags))
2726 return;
2727
2728 while (!list_empty(&vha->work_list) && i > 0) {
2729 qla2x00_do_work(vha);
2730 i--;
2731 }
2732
2733 spin_lock_irqsave(&vha->work_lock, flags);
2734 clear_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags);
2735 spin_unlock_irqrestore(&vha->work_lock, flags);
2736 }
2737
2738 /*
2739 * PCI driver interface
2740 */
2741 static int
2742 qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2743 {
2744 int ret = -ENODEV;
2745 struct Scsi_Host *host;
2746 scsi_qla_host_t *base_vha = NULL;
2747 struct qla_hw_data *ha;
2748 char pci_info[30];
2749 char fw_str[30], wq_name[30];
2750 struct scsi_host_template *sht;
2751 int bars, mem_only = 0;
2752 uint16_t req_length = 0, rsp_length = 0;
2753 struct req_que *req = NULL;
2754 struct rsp_que *rsp = NULL;
2755 int i;
2756
2757 bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
2758 sht = &qla2xxx_driver_template;
2759 if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
2760 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
2761 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
2762 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
2763 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
2764 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
2765 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001 ||
2766 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8021 ||
2767 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2031 ||
2768 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8031 ||
2769 pdev->device == PCI_DEVICE_ID_QLOGIC_ISPF001 ||
2770 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8044 ||
2771 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2071 ||
2772 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2271 ||
2773 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2261 ||
2774 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2081 ||
2775 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2281 ||
2776 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2089 ||
2777 pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2289) {
2778 bars = pci_select_bars(pdev, IORESOURCE_MEM);
2779 mem_only = 1;
2780 ql_dbg_pci(ql_dbg_init, pdev, 0x0007,
2781 "Mem only adapter.\n");
2782 }
2783 ql_dbg_pci(ql_dbg_init, pdev, 0x0008,
2784 "Bars=%d.\n", bars);
2785
2786 if (mem_only) {
2787 if (pci_enable_device_mem(pdev))
2788 return ret;
2789 } else {
2790 if (pci_enable_device(pdev))
2791 return ret;
2792 }
2793
2794 /* This may fail but that's ok */
2795 pci_enable_pcie_error_reporting(pdev);
2796
2797 /* Turn off T10-DIF when FC-NVMe is enabled */
2798 if (ql2xnvmeenable)
2799 ql2xenabledif = 0;
2800
2801 ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
2802 if (!ha) {
2803 ql_log_pci(ql_log_fatal, pdev, 0x0009,
2804 "Unable to allocate memory for ha.\n");
2805 goto disable_device;
2806 }
2807 ql_dbg_pci(ql_dbg_init, pdev, 0x000a,
2808 "Memory allocated for ha=%p.\n", ha);
2809 ha->pdev = pdev;
2810 INIT_LIST_HEAD(&ha->tgt.q_full_list);
2811 spin_lock_init(&ha->tgt.q_full_lock);
2812 spin_lock_init(&ha->tgt.sess_lock);
2813 spin_lock_init(&ha->tgt.atio_lock);
2814
2815 atomic_set(&ha->nvme_active_aen_cnt, 0);
2816
2817 /* Clear our data area */
2818 ha->bars = bars;
2819 ha->mem_only = mem_only;
2820 spin_lock_init(&ha->hardware_lock);
2821 spin_lock_init(&ha->vport_slock);
2822 mutex_init(&ha->selflogin_lock);
2823 mutex_init(&ha->optrom_mutex);
2824
2825 /* Set ISP-type information. */
2826 qla2x00_set_isp_flags(ha);
2827
2828 /* Set EEH reset type to fundamental if required by hba */
2829 if (IS_QLA24XX(ha) || IS_QLA25XX(ha) || IS_QLA81XX(ha) ||
2830 IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
2831 pdev->needs_freset = 1;
2832
2833 ha->prev_topology = 0;
2834 ha->init_cb_size = sizeof(init_cb_t);
2835 ha->link_data_rate = PORT_SPEED_UNKNOWN;
2836 ha->optrom_size = OPTROM_SIZE_2300;
2837 ha->max_exchg = FW_MAX_EXCHANGES_CNT;
2838 atomic_set(&ha->num_pend_mbx_stage1, 0);
2839 atomic_set(&ha->num_pend_mbx_stage2, 0);
2840 atomic_set(&ha->num_pend_mbx_stage3, 0);
2841 atomic_set(&ha->zio_threshold, DEFAULT_ZIO_THRESHOLD);
2842 ha->last_zio_threshold = DEFAULT_ZIO_THRESHOLD;
2843
2844 /* Assign ISP specific operations. */
2845 if (IS_QLA2100(ha)) {
2846 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2847 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
2848 req_length = REQUEST_ENTRY_CNT_2100;
2849 rsp_length = RESPONSE_ENTRY_CNT_2100;
2850 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2851 ha->gid_list_info_size = 4;
2852 ha->flash_conf_off = ~0;
2853 ha->flash_data_off = ~0;
2854 ha->nvram_conf_off = ~0;
2855 ha->nvram_data_off = ~0;
2856 ha->isp_ops = &qla2100_isp_ops;
2857 } else if (IS_QLA2200(ha)) {
2858 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2859 ha->mbx_count = MAILBOX_REGISTER_COUNT_2200;
2860 req_length = REQUEST_ENTRY_CNT_2200;
2861 rsp_length = RESPONSE_ENTRY_CNT_2100;
2862 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
2863 ha->gid_list_info_size = 4;
2864 ha->flash_conf_off = ~0;
2865 ha->flash_data_off = ~0;
2866 ha->nvram_conf_off = ~0;
2867 ha->nvram_data_off = ~0;
2868 ha->isp_ops = &qla2100_isp_ops;
2869 } else if (IS_QLA23XX(ha)) {
2870 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2100;
2871 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2872 req_length = REQUEST_ENTRY_CNT_2200;
2873 rsp_length = RESPONSE_ENTRY_CNT_2300;
2874 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2875 ha->gid_list_info_size = 6;
2876 if (IS_QLA2322(ha) || IS_QLA6322(ha))
2877 ha->optrom_size = OPTROM_SIZE_2322;
2878 ha->flash_conf_off = ~0;
2879 ha->flash_data_off = ~0;
2880 ha->nvram_conf_off = ~0;
2881 ha->nvram_data_off = ~0;
2882 ha->isp_ops = &qla2300_isp_ops;
2883 } else if (IS_QLA24XX_TYPE(ha)) {
2884 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2885 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2886 req_length = REQUEST_ENTRY_CNT_24XX;
2887 rsp_length = RESPONSE_ENTRY_CNT_2300;
2888 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2889 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2890 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2891 ha->gid_list_info_size = 8;
2892 ha->optrom_size = OPTROM_SIZE_24XX;
2893 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
2894 ha->isp_ops = &qla24xx_isp_ops;
2895 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2896 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2897 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2898 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2899 } else if (IS_QLA25XX(ha)) {
2900 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2901 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2902 req_length = REQUEST_ENTRY_CNT_24XX;
2903 rsp_length = RESPONSE_ENTRY_CNT_2300;
2904 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2905 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2906 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
2907 ha->gid_list_info_size = 8;
2908 ha->optrom_size = OPTROM_SIZE_25XX;
2909 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2910 ha->isp_ops = &qla25xx_isp_ops;
2911 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2912 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2913 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2914 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2915 } else if (IS_QLA81XX(ha)) {
2916 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2917 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2918 req_length = REQUEST_ENTRY_CNT_24XX;
2919 rsp_length = RESPONSE_ENTRY_CNT_2300;
2920 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2921 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2922 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2923 ha->gid_list_info_size = 8;
2924 ha->optrom_size = OPTROM_SIZE_81XX;
2925 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2926 ha->isp_ops = &qla81xx_isp_ops;
2927 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2928 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2929 ha->nvram_conf_off = ~0;
2930 ha->nvram_data_off = ~0;
2931 } else if (IS_QLA82XX(ha)) {
2932 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2933 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2934 req_length = REQUEST_ENTRY_CNT_82XX;
2935 rsp_length = RESPONSE_ENTRY_CNT_82XX;
2936 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2937 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2938 ha->gid_list_info_size = 8;
2939 ha->optrom_size = OPTROM_SIZE_82XX;
2940 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2941 ha->isp_ops = &qla82xx_isp_ops;
2942 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2943 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2944 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2945 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2946 } else if (IS_QLA8044(ha)) {
2947 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2948 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2949 req_length = REQUEST_ENTRY_CNT_82XX;
2950 rsp_length = RESPONSE_ENTRY_CNT_82XX;
2951 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2952 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2953 ha->gid_list_info_size = 8;
2954 ha->optrom_size = OPTROM_SIZE_83XX;
2955 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2956 ha->isp_ops = &qla8044_isp_ops;
2957 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
2958 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
2959 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
2960 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
2961 } else if (IS_QLA83XX(ha)) {
2962 ha->portnum = PCI_FUNC(ha->pdev->devfn);
2963 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2964 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2965 req_length = REQUEST_ENTRY_CNT_83XX;
2966 rsp_length = RESPONSE_ENTRY_CNT_83XX;
2967 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2968 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
2969 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
2970 ha->gid_list_info_size = 8;
2971 ha->optrom_size = OPTROM_SIZE_83XX;
2972 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
2973 ha->isp_ops = &qla83xx_isp_ops;
2974 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
2975 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
2976 ha->nvram_conf_off = ~0;
2977 ha->nvram_data_off = ~0;
2978 } else if (IS_QLAFX00(ha)) {
2979 ha->max_fibre_devices = MAX_FIBRE_DEVICES_FX00;
2980 ha->mbx_count = MAILBOX_REGISTER_COUNT_FX00;
2981 ha->aen_mbx_count = AEN_MAILBOX_REGISTER_COUNT_FX00;
2982 req_length = REQUEST_ENTRY_CNT_FX00;
2983 rsp_length = RESPONSE_ENTRY_CNT_FX00;
2984 ha->isp_ops = &qlafx00_isp_ops;
2985 ha->port_down_retry_count = 30; /* default value */
2986 ha->mr.fw_hbt_cnt = QLAFX00_HEARTBEAT_INTERVAL;
2987 ha->mr.fw_reset_timer_tick = QLAFX00_RESET_INTERVAL;
2988 ha->mr.fw_critemp_timer_tick = QLAFX00_CRITEMP_INTERVAL;
2989 ha->mr.fw_hbt_en = 1;
2990 ha->mr.host_info_resend = false;
2991 ha->mr.hinfo_resend_timer_tick = QLAFX00_HINFO_RESEND_INTERVAL;
2992 } else if (IS_QLA27XX(ha)) {
2993 ha->portnum = PCI_FUNC(ha->pdev->devfn);
2994 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
2995 ha->mbx_count = MAILBOX_REGISTER_COUNT;
2996 req_length = REQUEST_ENTRY_CNT_83XX;
2997 rsp_length = RESPONSE_ENTRY_CNT_83XX;
2998 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
2999 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3000 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3001 ha->gid_list_info_size = 8;
3002 ha->optrom_size = OPTROM_SIZE_83XX;
3003 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3004 ha->isp_ops = &qla27xx_isp_ops;
3005 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
3006 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
3007 ha->nvram_conf_off = ~0;
3008 ha->nvram_data_off = ~0;
3009 } else if (IS_QLA28XX(ha)) {
3010 ha->portnum = PCI_FUNC(ha->pdev->devfn);
3011 ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
3012 ha->mbx_count = MAILBOX_REGISTER_COUNT;
3013 req_length = REQUEST_ENTRY_CNT_24XX;
3014 rsp_length = RESPONSE_ENTRY_CNT_2300;
3015 ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
3016 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
3017 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
3018 ha->gid_list_info_size = 8;
3019 ha->optrom_size = OPTROM_SIZE_28XX;
3020 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
3021 ha->isp_ops = &qla27xx_isp_ops;
3022 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_28XX;
3023 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_28XX;
3024 ha->nvram_conf_off = ~0;
3025 ha->nvram_data_off = ~0;
3026 }
3027
3028 ql_dbg_pci(ql_dbg_init, pdev, 0x001e,
3029 "mbx_count=%d, req_length=%d, "
3030 "rsp_length=%d, max_loop_id=%d, init_cb_size=%d, "
3031 "gid_list_info_size=%d, optrom_size=%d, nvram_npiv_size=%d, "
3032 "max_fibre_devices=%d.\n",
3033 ha->mbx_count, req_length, rsp_length, ha->max_loop_id,
3034 ha->init_cb_size, ha->gid_list_info_size, ha->optrom_size,
3035 ha->nvram_npiv_size, ha->max_fibre_devices);
3036 ql_dbg_pci(ql_dbg_init, pdev, 0x001f,
3037 "isp_ops=%p, flash_conf_off=%d, "
3038 "flash_data_off=%d, nvram_conf_off=%d, nvram_data_off=%d.\n",
3039 ha->isp_ops, ha->flash_conf_off, ha->flash_data_off,
3040 ha->nvram_conf_off, ha->nvram_data_off);
3041
3042 /* Configure PCI I/O space */
3043 ret = ha->isp_ops->iospace_config(ha);
3044 if (ret)
3045 goto iospace_config_failed;
3046
3047 ql_log_pci(ql_log_info, pdev, 0x001d,
3048 "Found an ISP%04X irq %d iobase 0x%p.\n",
3049 pdev->device, pdev->irq, ha->iobase);
3050 mutex_init(&ha->vport_lock);
3051 mutex_init(&ha->mq_lock);
3052 init_completion(&ha->mbx_cmd_comp);
3053 complete(&ha->mbx_cmd_comp);
3054 init_completion(&ha->mbx_intr_comp);
3055 init_completion(&ha->dcbx_comp);
3056 init_completion(&ha->lb_portup_comp);
3057
3058 set_bit(0, (unsigned long *) ha->vp_idx_map);
3059
3060 qla2x00_config_dma_addressing(ha);
3061 ql_dbg_pci(ql_dbg_init, pdev, 0x0020,
3062 "64 Bit addressing is %s.\n",
3063 ha->flags.enable_64bit_addressing ? "enable" :
3064 "disable");
3065 ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
3066 if (ret) {
3067 ql_log_pci(ql_log_fatal, pdev, 0x0031,
3068 "Failed to allocate memory for adapter, aborting.\n");
3069
3070 goto probe_hw_failed;
3071 }
3072
3073 req->max_q_depth = MAX_Q_DEPTH;
3074 if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
3075 req->max_q_depth = ql2xmaxqdepth;
3076
3077
3078 base_vha = qla2x00_create_host(sht, ha);
3079 if (!base_vha) {
3080 ret = -ENOMEM;
3081 goto probe_hw_failed;
3082 }
3083
3084 pci_set_drvdata(pdev, base_vha);
3085 set_bit(PFLG_DRIVER_PROBING, &base_vha->pci_flags);
3086
3087 host = base_vha->host;
3088 base_vha->req = req;
3089 if (IS_QLA2XXX_MIDTYPE(ha))
3090 base_vha->mgmt_svr_loop_id =
3091 qla2x00_reserve_mgmt_server_loop_id(base_vha);
3092 else
3093 base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
3094 base_vha->vp_idx;
3095
3096 /* Setup fcport template structure. */
3097 ha->mr.fcport.vha = base_vha;
3098 ha->mr.fcport.port_type = FCT_UNKNOWN;
3099 ha->mr.fcport.loop_id = FC_NO_LOOP_ID;
3100 qla2x00_set_fcport_state(&ha->mr.fcport, FCS_UNCONFIGURED);
3101 ha->mr.fcport.supported_classes = FC_COS_UNSPECIFIED;
3102 ha->mr.fcport.scan_state = 1;
3103
3104 /* Set the SG table size based on ISP type */
3105 if (!IS_FWI2_CAPABLE(ha)) {
3106 if (IS_QLA2100(ha))
3107 host->sg_tablesize = 32;
3108 } else {
3109 if (!IS_QLA82XX(ha))
3110 host->sg_tablesize = QLA_SG_ALL;
3111 }
3112 host->max_id = ha->max_fibre_devices;
3113 host->cmd_per_lun = 3;
3114 host->unique_id = host->host_no;
3115 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
3116 host->max_cmd_len = 32;
3117 else
3118 host->max_cmd_len = MAX_CMDSZ;
3119 host->max_channel = MAX_BUSES - 1;
3120 /* Older HBAs support only 16-bit LUNs */
3121 if (!IS_QLAFX00(ha) && !IS_FWI2_CAPABLE(ha) &&
3122 ql2xmaxlun > 0xffff)
3123 host->max_lun = 0xffff;
3124 else
3125 host->max_lun = ql2xmaxlun;
3126 host->transportt = qla2xxx_transport_template;
3127 sht->vendor_id = (SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_QLOGIC);
3128
3129 ql_dbg(ql_dbg_init, base_vha, 0x0033,
3130 "max_id=%d this_id=%d "
3131 "cmd_per_len=%d unique_id=%d max_cmd_len=%d max_channel=%d "
3132 "max_lun=%llu transportt=%p, vendor_id=%llu.\n", host->max_id,
3133 host->this_id, host->cmd_per_lun, host->unique_id,
3134 host->max_cmd_len, host->max_channel, host->max_lun,
3135 host->transportt, sht->vendor_id);
3136
3137 INIT_WORK(&base_vha->iocb_work, qla2x00_iocb_work_fn);
3138
3139 /* Set up the irqs */
3140 ret = qla2x00_request_irqs(ha, rsp);
3141 if (ret)
3142 goto probe_failed;
3143
3144 /* Alloc arrays of request and response ring ptrs */
3145 ret = qla2x00_alloc_queues(ha, req, rsp);
3146 if (ret) {
3147 ql_log(ql_log_fatal, base_vha, 0x003d,
3148 "Failed to allocate memory for queue pointers..."
3149 "aborting.\n");
3150 ret = -ENODEV;
3151 goto probe_failed;
3152 }
3153
3154 if (ha->mqenable) {
3155 /* number of hardware queues supported by blk/scsi-mq*/
3156 host->nr_hw_queues = ha->max_qpairs;
3157
3158 ql_dbg(ql_dbg_init, base_vha, 0x0192,
3159 "blk/scsi-mq enabled, HW queues = %d.\n", host->nr_hw_queues);
3160 } else {
3161 if (ql2xnvmeenable) {
3162 host->nr_hw_queues = ha->max_qpairs;
3163 ql_dbg(ql_dbg_init, base_vha, 0x0194,
3164 "FC-NVMe support is enabled, HW queues=%d\n",
3165 host->nr_hw_queues);
3166 } else {
3167 ql_dbg(ql_dbg_init, base_vha, 0x0193,
3168 "blk/scsi-mq disabled.\n");
3169 }
3170 }
3171
3172 qlt_probe_one_stage1(base_vha, ha);
3173
3174 pci_save_state(pdev);
3175
3176 /* Assign back pointers */
3177 rsp->req = req;
3178 req->rsp = rsp;
3179
3180 if (IS_QLAFX00(ha)) {
3181 ha->rsp_q_map[0] = rsp;
3182 ha->req_q_map[0] = req;
3183 set_bit(0, ha->req_qid_map);
3184 set_bit(0, ha->rsp_qid_map);
3185 }
3186
3187 /* FWI2-capable only. */
3188 req->req_q_in = &ha->iobase->isp24.req_q_in;
3189 req->req_q_out = &ha->iobase->isp24.req_q_out;
3190 rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
3191 rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
3192 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
3193 IS_QLA28XX(ha)) {
3194 req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
3195 req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
3196 rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
3197 rsp->rsp_q_out = &ha->mqiobase->isp25mq.rsp_q_out;
3198 }
3199
3200 if (IS_QLAFX00(ha)) {
3201 req->req_q_in = &ha->iobase->ispfx00.req_q_in;
3202 req->req_q_out = &ha->iobase->ispfx00.req_q_out;
3203 rsp->rsp_q_in = &ha->iobase->ispfx00.rsp_q_in;
3204 rsp->rsp_q_out = &ha->iobase->ispfx00.rsp_q_out;
3205 }
3206
3207 if (IS_P3P_TYPE(ha)) {
3208 req->req_q_out = &ha->iobase->isp82.req_q_out[0];
3209 rsp->rsp_q_in = &ha->iobase->isp82.rsp_q_in[0];
3210 rsp->rsp_q_out = &ha->iobase->isp82.rsp_q_out[0];
3211 }
3212
3213 ql_dbg(ql_dbg_multiq, base_vha, 0xc009,
3214 "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
3215 ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
3216 ql_dbg(ql_dbg_multiq, base_vha, 0xc00a,
3217 "req->req_q_in=%p req->req_q_out=%p "
3218 "rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
3219 req->req_q_in, req->req_q_out,
3220 rsp->rsp_q_in, rsp->rsp_q_out);
3221 ql_dbg(ql_dbg_init, base_vha, 0x003e,
3222 "rsp_q_map=%p req_q_map=%p rsp->req=%p req->rsp=%p.\n",
3223 ha->rsp_q_map, ha->req_q_map, rsp->req, req->rsp);
3224 ql_dbg(ql_dbg_init, base_vha, 0x003f,
3225 "req->req_q_in=%p req->req_q_out=%p rsp->rsp_q_in=%p rsp->rsp_q_out=%p.\n",
3226 req->req_q_in, req->req_q_out, rsp->rsp_q_in, rsp->rsp_q_out);
3227
3228 ha->wq = alloc_workqueue("qla2xxx_wq", 0, 0);
3229
3230 if (ha->isp_ops->initialize_adapter(base_vha)) {
3231 ql_log(ql_log_fatal, base_vha, 0x00d6,
3232 "Failed to initialize adapter - Adapter flags %x.\n",
3233 base_vha->device_flags);
3234
3235 if (IS_QLA82XX(ha)) {
3236 qla82xx_idc_lock(ha);
3237 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
3238 QLA8XXX_DEV_FAILED);
3239 qla82xx_idc_unlock(ha);
3240 ql_log(ql_log_fatal, base_vha, 0x00d7,
3241 "HW State: FAILED.\n");
3242 } else if (IS_QLA8044(ha)) {
3243 qla8044_idc_lock(ha);
3244 qla8044_wr_direct(base_vha,
3245 QLA8044_CRB_DEV_STATE_INDEX,
3246 QLA8XXX_DEV_FAILED);
3247 qla8044_idc_unlock(ha);
3248 ql_log(ql_log_fatal, base_vha, 0x0150,
3249 "HW State: FAILED.\n");
3250 }
3251
3252 ret = -ENODEV;
3253 goto probe_failed;
3254 }
3255
3256 if (IS_QLAFX00(ha))
3257 host->can_queue = QLAFX00_MAX_CANQUEUE;
3258 else
3259 host->can_queue = req->num_outstanding_cmds - 10;
3260
3261 ql_dbg(ql_dbg_init, base_vha, 0x0032,
3262 "can_queue=%d, req=%p, mgmt_svr_loop_id=%d, sg_tablesize=%d.\n",
3263 host->can_queue, base_vha->req,
3264 base_vha->mgmt_svr_loop_id, host->sg_tablesize);
3265
3266 if (ha->mqenable) {
3267 bool startit = false;
3268
3269 if (QLA_TGT_MODE_ENABLED())
3270 startit = false;
3271
3272 if (ql2x_ini_mode == QLA2XXX_INI_MODE_ENABLED)
3273 startit = true;
3274
3275 /* Create start of day qpairs for Block MQ */
3276 for (i = 0; i < ha->max_qpairs; i++)
3277 qla2xxx_create_qpair(base_vha, 5, 0, startit);
3278 }
3279
3280 if (ha->flags.running_gold_fw)
3281 goto skip_dpc;
3282
3283 /*
3284 * Startup the kernel thread for this host adapter
3285 */
3286 ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
3287 "%s_dpc", base_vha->host_str);
3288 if (IS_ERR(ha->dpc_thread)) {
3289 ql_log(ql_log_fatal, base_vha, 0x00ed,
3290 "Failed to start DPC thread.\n");
3291 ret = PTR_ERR(ha->dpc_thread);
3292 ha->dpc_thread = NULL;
3293 goto probe_failed;
3294 }
3295 ql_dbg(ql_dbg_init, base_vha, 0x00ee,
3296 "DPC thread started successfully.\n");
3297
3298 /*
3299 * If we're not coming up in initiator mode, we might sit for
3300 * a while without waking up the dpc thread, which leads to a
3301 * stuck process warning. So just kick the dpc once here and
3302 * let the kthread start (and go back to sleep in qla2x00_do_dpc).
3303 */
3304 qla2xxx_wake_dpc(base_vha);
3305
3306 INIT_WORK(&ha->board_disable, qla2x00_disable_board_on_pci_error);
3307
3308 if (IS_QLA8031(ha) || IS_MCTP_CAPABLE(ha)) {
3309 sprintf(wq_name, "qla2xxx_%lu_dpc_lp_wq", base_vha->host_no);
3310 ha->dpc_lp_wq = create_singlethread_workqueue(wq_name);
3311 INIT_WORK(&ha->idc_aen, qla83xx_service_idc_aen);
3312
3313 sprintf(wq_name, "qla2xxx_%lu_dpc_hp_wq", base_vha->host_no);
3314 ha->dpc_hp_wq = create_singlethread_workqueue(wq_name);
3315 INIT_WORK(&ha->nic_core_reset, qla83xx_nic_core_reset_work);
3316 INIT_WORK(&ha->idc_state_handler,
3317 qla83xx_idc_state_handler_work);
3318 INIT_WORK(&ha->nic_core_unrecoverable,
3319 qla83xx_nic_core_unrecoverable_work);
3320 }
3321
3322 skip_dpc:
3323 list_add_tail(&base_vha->list, &ha->vp_list);
3324 base_vha->host->irq = ha->pdev->irq;
3325
3326 /* Initialized the timer */
3327 qla2x00_start_timer(base_vha, WATCH_INTERVAL);
3328 ql_dbg(ql_dbg_init, base_vha, 0x00ef,
3329 "Started qla2x00_timer with "
3330 "interval=%d.\n", WATCH_INTERVAL);
3331 ql_dbg(ql_dbg_init, base_vha, 0x00f0,
3332 "Detected hba at address=%p.\n",
3333 ha);
3334
3335 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
3336 if (ha->fw_attributes & BIT_4) {
3337 int prot = 0, guard;
3338
3339 base_vha->flags.difdix_supported = 1;
3340 ql_dbg(ql_dbg_init, base_vha, 0x00f1,
3341 "Registering for DIF/DIX type 1 and 3 protection.\n");
3342 if (ql2xenabledif == 1)
3343 prot = SHOST_DIX_TYPE0_PROTECTION;
3344 if (ql2xprotmask)
3345 scsi_host_set_prot(host, ql2xprotmask);
3346 else
3347 scsi_host_set_prot(host,
3348 prot | SHOST_DIF_TYPE1_PROTECTION
3349 | SHOST_DIF_TYPE2_PROTECTION
3350 | SHOST_DIF_TYPE3_PROTECTION
3351 | SHOST_DIX_TYPE1_PROTECTION
3352 | SHOST_DIX_TYPE2_PROTECTION
3353 | SHOST_DIX_TYPE3_PROTECTION);
3354
3355 guard = SHOST_DIX_GUARD_CRC;
3356
3357 if (IS_PI_IPGUARD_CAPABLE(ha) &&
3358 (ql2xenabledif > 1 || IS_PI_DIFB_DIX0_CAPABLE(ha)))
3359 guard |= SHOST_DIX_GUARD_IP;
3360
3361 if (ql2xprotguard)
3362 scsi_host_set_guard(host, ql2xprotguard);
3363 else
3364 scsi_host_set_guard(host, guard);
3365 } else
3366 base_vha->flags.difdix_supported = 0;
3367 }
3368
3369 ha->isp_ops->enable_intrs(ha);
3370
3371 if (IS_QLAFX00(ha)) {
3372 ret = qlafx00_fx_disc(base_vha,
3373 &base_vha->hw->mr.fcport, FXDISC_GET_CONFIG_INFO);
3374 host->sg_tablesize = (ha->mr.extended_io_enabled) ?
3375 QLA_SG_ALL : 128;
3376 }
3377
3378 ret = scsi_add_host(host, &pdev->dev);
3379 if (ret)
3380 goto probe_failed;
3381
3382 base_vha->flags.init_done = 1;
3383 base_vha->flags.online = 1;
3384 ha->prev_minidump_failed = 0;
3385
3386 ql_dbg(ql_dbg_init, base_vha, 0x00f2,
3387 "Init done and hba is online.\n");
3388
3389 if (qla_ini_mode_enabled(base_vha) ||
3390 qla_dual_mode_enabled(base_vha))
3391 scsi_scan_host(host);
3392 else
3393 ql_dbg(ql_dbg_init, base_vha, 0x0122,
3394 "skipping scsi_scan_host() for non-initiator port\n");
3395
3396 qla2x00_alloc_sysfs_attr(base_vha);
3397
3398 if (IS_QLAFX00(ha)) {
3399 ret = qlafx00_fx_disc(base_vha,
3400 &base_vha->hw->mr.fcport, FXDISC_GET_PORT_INFO);
3401
3402 /* Register system information */
3403 ret = qlafx00_fx_disc(base_vha,
3404 &base_vha->hw->mr.fcport, FXDISC_REG_HOST_INFO);
3405 }
3406
3407 qla2x00_init_host_attr(base_vha);
3408
3409 qla2x00_dfs_setup(base_vha);
3410
3411 ql_log(ql_log_info, base_vha, 0x00fb,
3412 "QLogic %s - %s.\n", ha->model_number, ha->model_desc);
3413 ql_log(ql_log_info, base_vha, 0x00fc,
3414 "ISP%04X: %s @ %s hdma%c host#=%ld fw=%s.\n",
3415 pdev->device, ha->isp_ops->pci_info_str(base_vha, pci_info,
3416 sizeof(pci_info)),
3417 pci_name(pdev), ha->flags.enable_64bit_addressing ? '+' : '-',
3418 base_vha->host_no,
3419 ha->isp_ops->fw_version_str(base_vha, fw_str, sizeof(fw_str)));
3420
3421 qlt_add_target(ha, base_vha);
3422
3423 clear_bit(PFLG_DRIVER_PROBING, &base_vha->pci_flags);
3424
3425 if (test_bit(UNLOADING, &base_vha->dpc_flags))
3426 return -ENODEV;
3427
3428 if (ha->flags.detected_lr_sfp) {
3429 ql_log(ql_log_info, base_vha, 0xffff,
3430 "Reset chip to pick up LR SFP setting\n");
3431 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
3432 qla2xxx_wake_dpc(base_vha);
3433 }
3434
3435 return 0;
3436
3437 probe_failed:
3438 if (base_vha->timer_active)
3439 qla2x00_stop_timer(base_vha);
3440 base_vha->flags.online = 0;
3441 if (ha->dpc_thread) {
3442 struct task_struct *t = ha->dpc_thread;
3443
3444 ha->dpc_thread = NULL;
3445 kthread_stop(t);
3446 }
3447
3448 qla2x00_free_device(base_vha);
3449 scsi_host_put(base_vha->host);
3450 /*
3451 * Need to NULL out local req/rsp after
3452 * qla2x00_free_device => qla2x00_free_queues frees
3453 * what these are pointing to. Or else we'll
3454 * fall over below in qla2x00_free_req/rsp_que.
3455 */
3456 req = NULL;
3457 rsp = NULL;
3458
3459 probe_hw_failed:
3460 qla2x00_mem_free(ha);
3461 qla2x00_free_req_que(ha, req);
3462 qla2x00_free_rsp_que(ha, rsp);
3463 qla2x00_clear_drv_active(ha);
3464
3465 iospace_config_failed:
3466 if (IS_P3P_TYPE(ha)) {
3467 if (!ha->nx_pcibase)
3468 iounmap((device_reg_t *)ha->nx_pcibase);
3469 if (!ql2xdbwr)
3470 iounmap((device_reg_t *)ha->nxdb_wr_ptr);
3471 } else {
3472 if (ha->iobase)
3473 iounmap(ha->iobase);
3474 if (ha->cregbase)
3475 iounmap(ha->cregbase);
3476 }
3477 pci_release_selected_regions(ha->pdev, ha->bars);
3478 kfree(ha);
3479
3480 disable_device:
3481 pci_disable_device(pdev);
3482 return ret;
3483 }
3484
3485 static void
3486 qla2x00_shutdown(struct pci_dev *pdev)
3487 {
3488 scsi_qla_host_t *vha;
3489 struct qla_hw_data *ha;
3490
3491 vha = pci_get_drvdata(pdev);
3492 ha = vha->hw;
3493
3494 ql_log(ql_log_info, vha, 0xfffa,
3495 "Adapter shutdown\n");
3496
3497 /*
3498 * Prevent future board_disable and wait
3499 * until any pending board_disable has completed.
3500 */
3501 set_bit(PFLG_DRIVER_REMOVING, &vha->pci_flags);
3502 cancel_work_sync(&ha->board_disable);
3503
3504 if (!atomic_read(&pdev->enable_cnt))
3505 return;
3506
3507 /* Notify ISPFX00 firmware */
3508 if (IS_QLAFX00(ha))
3509 qlafx00_driver_shutdown(vha, 20);
3510
3511 /* Turn-off FCE trace */
3512 if (ha->flags.fce_enabled) {
3513 qla2x00_disable_fce_trace(vha, NULL, NULL);
3514 ha->flags.fce_enabled = 0;
3515 }
3516
3517 /* Turn-off EFT trace */
3518 if (ha->eft)
3519 qla2x00_disable_eft_trace(vha);
3520
3521 if (IS_QLA25XX(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
3522 IS_QLA28XX(ha)) {
3523 if (ha->flags.fw_started)
3524 qla2x00_abort_isp_cleanup(vha);
3525 } else {
3526 /* Stop currently executing firmware. */
3527 qla2x00_try_to_stop_firmware(vha);
3528 }
3529
3530 /* Turn adapter off line */
3531 vha->flags.online = 0;
3532
3533 /* turn-off interrupts on the card */
3534 if (ha->interrupts_on) {
3535 vha->flags.init_done = 0;
3536 ha->isp_ops->disable_intrs(ha);
3537 }
3538
3539 qla2x00_free_irqs(vha);
3540
3541 qla2x00_free_fw_dump(ha);
3542
3543 pci_disable_device(pdev);
3544 ql_log(ql_log_info, vha, 0xfffe,
3545 "Adapter shutdown successfully.\n");
3546 }
3547
3548 /* Deletes all the virtual ports for a given ha */
3549 static void
3550 qla2x00_delete_all_vps(struct qla_hw_data *ha, scsi_qla_host_t *base_vha)
3551 {
3552 scsi_qla_host_t *vha;
3553 unsigned long flags;
3554
3555 mutex_lock(&ha->vport_lock);
3556 while (ha->cur_vport_count) {
3557 spin_lock_irqsave(&ha->vport_slock, flags);
3558
3559 BUG_ON(base_vha->list.next == &ha->vp_list);
3560 /* This assumes first entry in ha->vp_list is always base vha */
3561 vha = list_first_entry(&base_vha->list, scsi_qla_host_t, list);
3562 scsi_host_get(vha->host);
3563
3564 spin_unlock_irqrestore(&ha->vport_slock, flags);
3565 mutex_unlock(&ha->vport_lock);
3566
3567 qla_nvme_delete(vha);
3568
3569 fc_vport_terminate(vha->fc_vport);
3570 scsi_host_put(vha->host);
3571
3572 mutex_lock(&ha->vport_lock);
3573 }
3574 mutex_unlock(&ha->vport_lock);
3575 }
3576
3577 /* Stops all deferred work threads */
3578 static void
3579 qla2x00_destroy_deferred_work(struct qla_hw_data *ha)
3580 {
3581 /* Cancel all work and destroy DPC workqueues */
3582 if (ha->dpc_lp_wq) {
3583 cancel_work_sync(&ha->idc_aen);
3584 destroy_workqueue(ha->dpc_lp_wq);
3585 ha->dpc_lp_wq = NULL;
3586 }
3587
3588 if (ha->dpc_hp_wq) {
3589 cancel_work_sync(&ha->nic_core_reset);
3590 cancel_work_sync(&ha->idc_state_handler);
3591 cancel_work_sync(&ha->nic_core_unrecoverable);
3592 destroy_workqueue(ha->dpc_hp_wq);
3593 ha->dpc_hp_wq = NULL;
3594 }
3595
3596 /* Kill the kernel thread for this host */
3597 if (ha->dpc_thread) {
3598 struct task_struct *t = ha->dpc_thread;
3599
3600 /*
3601 * qla2xxx_wake_dpc checks for ->dpc_thread
3602 * so we need to zero it out.
3603 */
3604 ha->dpc_thread = NULL;
3605 kthread_stop(t);
3606 }
3607 }
3608
3609 static void
3610 qla2x00_unmap_iobases(struct qla_hw_data *ha)
3611 {
3612 if (IS_QLA82XX(ha)) {
3613
3614 iounmap((device_reg_t *)ha->nx_pcibase);
3615 if (!ql2xdbwr)
3616 iounmap((device_reg_t *)ha->nxdb_wr_ptr);
3617 } else {
3618 if (ha->iobase)
3619 iounmap(ha->iobase);
3620
3621 if (ha->cregbase)
3622 iounmap(ha->cregbase);
3623
3624 if (ha->mqiobase)
3625 iounmap(ha->mqiobase);
3626
3627 if ((IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) &&
3628 ha->msixbase)
3629 iounmap(ha->msixbase);
3630 }
3631 }
3632
3633 static void
3634 qla2x00_clear_drv_active(struct qla_hw_data *ha)
3635 {
3636 if (IS_QLA8044(ha)) {
3637 qla8044_idc_lock(ha);
3638 qla8044_clear_drv_active(ha);
3639 qla8044_idc_unlock(ha);
3640 } else if (IS_QLA82XX(ha)) {
3641 qla82xx_idc_lock(ha);
3642 qla82xx_clear_drv_active(ha);
3643 qla82xx_idc_unlock(ha);
3644 }
3645 }
3646
3647 static void
3648 qla2x00_remove_one(struct pci_dev *pdev)
3649 {
3650 scsi_qla_host_t *base_vha;
3651 struct qla_hw_data *ha;
3652
3653 base_vha = pci_get_drvdata(pdev);
3654 ha = base_vha->hw;
3655 ql_log(ql_log_info, base_vha, 0xb079,
3656 "Removing driver\n");
3657
3658 /* Indicate device removal to prevent future board_disable and wait
3659 * until any pending board_disable has completed. */
3660 set_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags);
3661 cancel_work_sync(&ha->board_disable);
3662
3663 /*
3664 * If the PCI device is disabled then there was a PCI-disconnect and
3665 * qla2x00_disable_board_on_pci_error has taken care of most of the
3666 * resources.
3667 */
3668 if (!atomic_read(&pdev->enable_cnt)) {
3669 dma_free_coherent(&ha->pdev->dev, base_vha->gnl.size,
3670 base_vha->gnl.l, base_vha->gnl.ldma);
3671
3672 scsi_host_put(base_vha->host);
3673 kfree(ha);
3674 pci_set_drvdata(pdev, NULL);
3675 return;
3676 }
3677 qla2x00_wait_for_hba_ready(base_vha);
3678
3679 if (IS_QLA25XX(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
3680 IS_QLA28XX(ha)) {
3681 if (ha->flags.fw_started)
3682 qla2x00_abort_isp_cleanup(base_vha);
3683 } else if (!IS_QLAFX00(ha)) {
3684 if (IS_QLA8031(ha)) {
3685 ql_dbg(ql_dbg_p3p, base_vha, 0xb07e,
3686 "Clearing fcoe driver presence.\n");
3687 if (qla83xx_clear_drv_presence(base_vha) != QLA_SUCCESS)
3688 ql_dbg(ql_dbg_p3p, base_vha, 0xb079,
3689 "Error while clearing DRV-Presence.\n");
3690 }
3691
3692 qla2x00_try_to_stop_firmware(base_vha);
3693 }
3694
3695 qla2x00_wait_for_sess_deletion(base_vha);
3696
3697 /*
3698 * if UNLOAD flag is already set, then continue unload,
3699 * where it was set first.
3700 */
3701 if (test_bit(UNLOADING, &base_vha->dpc_flags))
3702 return;
3703
3704 set_bit(UNLOADING, &base_vha->dpc_flags);
3705
3706 qla_nvme_delete(base_vha);
3707
3708 dma_free_coherent(&ha->pdev->dev,
3709 base_vha->gnl.size, base_vha->gnl.l, base_vha->gnl.ldma);
3710
3711 vfree(base_vha->scan.l);
3712
3713 if (IS_QLAFX00(ha))
3714 qlafx00_driver_shutdown(base_vha, 20);
3715
3716 qla2x00_delete_all_vps(ha, base_vha);
3717
3718 qla2x00_dfs_remove(base_vha);
3719
3720 qla84xx_put_chip(base_vha);
3721
3722 /* Disable timer */
3723 if (base_vha->timer_active)
3724 qla2x00_stop_timer(base_vha);
3725
3726 base_vha->flags.online = 0;
3727
3728 /* free DMA memory */
3729 if (ha->exlogin_buf)
3730 qla2x00_free_exlogin_buffer(ha);
3731
3732 /* free DMA memory */
3733 if (ha->exchoffld_buf)
3734 qla2x00_free_exchoffld_buffer(ha);
3735
3736 qla2x00_destroy_deferred_work(ha);
3737
3738 qlt_remove_target(ha, base_vha);
3739
3740 qla2x00_free_sysfs_attr(base_vha, true);
3741
3742 fc_remove_host(base_vha->host);
3743 qlt_remove_target_resources(ha);
3744
3745 scsi_remove_host(base_vha->host);
3746
3747 qla2x00_free_device(base_vha);
3748
3749 qla2x00_clear_drv_active(ha);
3750
3751 scsi_host_put(base_vha->host);
3752
3753 qla2x00_unmap_iobases(ha);
3754
3755 pci_release_selected_regions(ha->pdev, ha->bars);
3756 kfree(ha);
3757
3758 pci_disable_pcie_error_reporting(pdev);
3759
3760 pci_disable_device(pdev);
3761 }
3762
3763 static void
3764 qla2x00_free_device(scsi_qla_host_t *vha)
3765 {
3766 struct qla_hw_data *ha = vha->hw;
3767
3768 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
3769
3770 /* Disable timer */
3771 if (vha->timer_active)
3772 qla2x00_stop_timer(vha);
3773
3774 qla25xx_delete_queues(vha);
3775 vha->flags.online = 0;
3776
3777 /* turn-off interrupts on the card */
3778 if (ha->interrupts_on) {
3779 vha->flags.init_done = 0;
3780 ha->isp_ops->disable_intrs(ha);
3781 }
3782
3783 qla2x00_free_fcports(vha);
3784
3785 qla2x00_free_irqs(vha);
3786
3787 /* Flush the work queue and remove it */
3788 if (ha->wq) {
3789 flush_workqueue(ha->wq);
3790 destroy_workqueue(ha->wq);
3791 ha->wq = NULL;
3792 }
3793
3794
3795 qla2x00_mem_free(ha);
3796
3797 qla82xx_md_free(vha);
3798
3799 qla2x00_free_queues(ha);
3800 }
3801
3802 void qla2x00_free_fcports(struct scsi_qla_host *vha)
3803 {
3804 fc_port_t *fcport, *tfcport;
3805
3806 list_for_each_entry_safe(fcport, tfcport, &vha->vp_fcports, list)
3807 qla2x00_free_fcport(fcport);
3808 }
3809
3810 static inline void
3811 qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
3812 int defer)
3813 {
3814 struct fc_rport *rport;
3815 scsi_qla_host_t *base_vha;
3816 unsigned long flags;
3817
3818 if (!fcport->rport)
3819 return;
3820
3821 rport = fcport->rport;
3822 if (defer) {
3823 base_vha = pci_get_drvdata(vha->hw->pdev);
3824 spin_lock_irqsave(vha->host->host_lock, flags);
3825 fcport->drport = rport;
3826 spin_unlock_irqrestore(vha->host->host_lock, flags);
3827 qlt_do_generation_tick(vha, &base_vha->total_fcport_update_gen);
3828 set_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
3829 qla2xxx_wake_dpc(base_vha);
3830 } else {
3831 int now;
3832
3833 if (rport) {
3834 ql_dbg(ql_dbg_disc, fcport->vha, 0x2109,
3835 "%s %8phN. rport %p roles %x\n",
3836 __func__, fcport->port_name, rport,
3837 rport->roles);
3838 fc_remote_port_delete(rport);
3839 }
3840 qlt_do_generation_tick(vha, &now);
3841 }
3842 }
3843
3844 /*
3845 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
3846 *
3847 * Input: ha = adapter block pointer. fcport = port structure pointer.
3848 *
3849 * Return: None.
3850 *
3851 * Context:
3852 */
3853 void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
3854 int do_login, int defer)
3855 {
3856 if (IS_QLAFX00(vha->hw)) {
3857 qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
3858 qla2x00_schedule_rport_del(vha, fcport, defer);
3859 return;
3860 }
3861
3862 if (atomic_read(&fcport->state) == FCS_ONLINE &&
3863 vha->vp_idx == fcport->vha->vp_idx) {
3864 qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
3865 qla2x00_schedule_rport_del(vha, fcport, defer);
3866 }
3867 /*
3868 * We may need to retry the login, so don't change the state of the
3869 * port but do the retries.
3870 */
3871 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
3872 qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
3873
3874 if (!do_login)
3875 return;
3876
3877 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
3878 }
3879
3880 /*
3881 * qla2x00_mark_all_devices_lost
3882 * Updates fcport state when device goes offline.
3883 *
3884 * Input:
3885 * ha = adapter block pointer.
3886 * fcport = port structure pointer.
3887 *
3888 * Return:
3889 * None.
3890 *
3891 * Context:
3892 */
3893 void
3894 qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
3895 {
3896 fc_port_t *fcport;
3897
3898 ql_dbg(ql_dbg_disc, vha, 0x20f1,
3899 "Mark all dev lost\n");
3900
3901 list_for_each_entry(fcport, &vha->vp_fcports, list) {
3902 fcport->scan_state = 0;
3903 qlt_schedule_sess_for_deletion(fcport);
3904
3905 if (vha->vp_idx != 0 && vha->vp_idx != fcport->vha->vp_idx)
3906 continue;
3907
3908 /*
3909 * No point in marking the device as lost, if the device is
3910 * already DEAD.
3911 */
3912 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
3913 continue;
3914 if (atomic_read(&fcport->state) == FCS_ONLINE) {
3915 qla2x00_set_fcport_state(fcport, FCS_DEVICE_LOST);
3916 if (defer)
3917 qla2x00_schedule_rport_del(vha, fcport, defer);
3918 else if (vha->vp_idx == fcport->vha->vp_idx)
3919 qla2x00_schedule_rport_del(vha, fcport, defer);
3920 }
3921 }
3922 }
3923
3924 static void qla2x00_set_reserved_loop_ids(struct qla_hw_data *ha)
3925 {
3926 int i;
3927
3928 if (IS_FWI2_CAPABLE(ha))
3929 return;
3930
3931 for (i = 0; i < SNS_FIRST_LOOP_ID; i++)
3932 set_bit(i, ha->loop_id_map);
3933 set_bit(MANAGEMENT_SERVER, ha->loop_id_map);
3934 set_bit(BROADCAST, ha->loop_id_map);
3935 }
3936
3937 /*
3938 * qla2x00_mem_alloc
3939 * Allocates adapter memory.
3940 *
3941 * Returns:
3942 * 0 = success.
3943 * !0 = failure.
3944 */
3945 static int
3946 qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
3947 struct req_que **req, struct rsp_que **rsp)
3948 {
3949 char name[16];
3950
3951 ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
3952 &ha->init_cb_dma, GFP_KERNEL);
3953 if (!ha->init_cb)
3954 goto fail;
3955
3956 if (qlt_mem_alloc(ha) < 0)
3957 goto fail_free_init_cb;
3958
3959 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev,
3960 qla2x00_gid_list_size(ha), &ha->gid_list_dma, GFP_KERNEL);
3961 if (!ha->gid_list)
3962 goto fail_free_tgt_mem;
3963
3964 ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
3965 if (!ha->srb_mempool)
3966 goto fail_free_gid_list;
3967
3968 if (IS_P3P_TYPE(ha)) {
3969 /* Allocate cache for CT6 Ctx. */
3970 if (!ctx_cachep) {
3971 ctx_cachep = kmem_cache_create("qla2xxx_ctx",
3972 sizeof(struct ct6_dsd), 0,
3973 SLAB_HWCACHE_ALIGN, NULL);
3974 if (!ctx_cachep)
3975 goto fail_free_srb_mempool;
3976 }
3977 ha->ctx_mempool = mempool_create_slab_pool(SRB_MIN_REQ,
3978 ctx_cachep);
3979 if (!ha->ctx_mempool)
3980 goto fail_free_srb_mempool;
3981 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0021,
3982 "ctx_cachep=%p ctx_mempool=%p.\n",
3983 ctx_cachep, ha->ctx_mempool);
3984 }
3985
3986 /* Get memory for cached NVRAM */
3987 ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
3988 if (!ha->nvram)
3989 goto fail_free_ctx_mempool;
3990
3991 snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
3992 ha->pdev->device);
3993 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
3994 DMA_POOL_SIZE, 8, 0);
3995 if (!ha->s_dma_pool)
3996 goto fail_free_nvram;
3997
3998 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0022,
3999 "init_cb=%p gid_list=%p, srb_mempool=%p s_dma_pool=%p.\n",
4000 ha->init_cb, ha->gid_list, ha->srb_mempool, ha->s_dma_pool);
4001
4002 if (IS_P3P_TYPE(ha) || ql2xenabledif) {
4003 ha->dl_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4004 DSD_LIST_DMA_POOL_SIZE, 8, 0);
4005 if (!ha->dl_dma_pool) {
4006 ql_log_pci(ql_log_fatal, ha->pdev, 0x0023,
4007 "Failed to allocate memory for dl_dma_pool.\n");
4008 goto fail_s_dma_pool;
4009 }
4010
4011 ha->fcp_cmnd_dma_pool = dma_pool_create(name, &ha->pdev->dev,
4012 FCP_CMND_DMA_POOL_SIZE, 8, 0);
4013 if (!ha->fcp_cmnd_dma_pool) {
4014 ql_log_pci(ql_log_fatal, ha->pdev, 0x0024,
4015 "Failed to allocate memory for fcp_cmnd_dma_pool.\n");
4016 goto fail_dl_dma_pool;
4017 }
4018
4019 if (ql2xenabledif) {
4020 u64 bufsize = DIF_BUNDLING_DMA_POOL_SIZE;
4021 struct dsd_dma *dsd, *nxt;
4022 uint i;
4023 /* Creata a DMA pool of buffers for DIF bundling */
4024 ha->dif_bundl_pool = dma_pool_create(name,
4025 &ha->pdev->dev, DIF_BUNDLING_DMA_POOL_SIZE, 8, 0);
4026 if (!ha->dif_bundl_pool) {
4027 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0024,
4028 "%s: failed create dif_bundl_pool\n",
4029 __func__);
4030 goto fail_dif_bundl_dma_pool;
4031 }
4032
4033 INIT_LIST_HEAD(&ha->pool.good.head);
4034 INIT_LIST_HEAD(&ha->pool.unusable.head);
4035 ha->pool.good.count = 0;
4036 ha->pool.unusable.count = 0;
4037 for (i = 0; i < 128; i++) {
4038 dsd = kzalloc(sizeof(*dsd), GFP_ATOMIC);
4039 if (!dsd) {
4040 ql_dbg_pci(ql_dbg_init, ha->pdev,
4041 0xe0ee, "%s: failed alloc dsd\n",
4042 __func__);
4043 return 1;
4044 }
4045 ha->dif_bundle_kallocs++;
4046
4047 dsd->dsd_addr = dma_pool_alloc(
4048 ha->dif_bundl_pool, GFP_ATOMIC,
4049 &dsd->dsd_list_dma);
4050 if (!dsd->dsd_addr) {
4051 ql_dbg_pci(ql_dbg_init, ha->pdev,
4052 0xe0ee,
4053 "%s: failed alloc ->dsd_addr\n",
4054 __func__);
4055 kfree(dsd);
4056 ha->dif_bundle_kallocs--;
4057 continue;
4058 }
4059 ha->dif_bundle_dma_allocs++;
4060
4061 /*
4062 * if DMA buffer crosses 4G boundary,
4063 * put it on bad list
4064 */
4065 if (MSD(dsd->dsd_list_dma) ^
4066 MSD(dsd->dsd_list_dma + bufsize)) {
4067 list_add_tail(&dsd->list,
4068 &ha->pool.unusable.head);
4069 ha->pool.unusable.count++;
4070 } else {
4071 list_add_tail(&dsd->list,
4072 &ha->pool.good.head);
4073 ha->pool.good.count++;
4074 }
4075 }
4076
4077 /* return the good ones back to the pool */
4078 list_for_each_entry_safe(dsd, nxt,
4079 &ha->pool.good.head, list) {
4080 list_del(&dsd->list);
4081 dma_pool_free(ha->dif_bundl_pool,
4082 dsd->dsd_addr, dsd->dsd_list_dma);
4083 ha->dif_bundle_dma_allocs--;
4084 kfree(dsd);
4085 ha->dif_bundle_kallocs--;
4086 }
4087
4088 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0024,
4089 "%s: dif dma pool (good=%u unusable=%u)\n",
4090 __func__, ha->pool.good.count,
4091 ha->pool.unusable.count);
4092 }
4093
4094 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0025,
4095 "dl_dma_pool=%p fcp_cmnd_dma_pool=%p dif_bundl_pool=%p.\n",
4096 ha->dl_dma_pool, ha->fcp_cmnd_dma_pool,
4097 ha->dif_bundl_pool);
4098 }
4099
4100 /* Allocate memory for SNS commands */
4101 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4102 /* Get consistent memory allocated for SNS commands */
4103 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
4104 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
4105 if (!ha->sns_cmd)
4106 goto fail_dma_pool;
4107 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0026,
4108 "sns_cmd: %p.\n", ha->sns_cmd);
4109 } else {
4110 /* Get consistent memory allocated for MS IOCB */
4111 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4112 &ha->ms_iocb_dma);
4113 if (!ha->ms_iocb)
4114 goto fail_dma_pool;
4115 /* Get consistent memory allocated for CT SNS commands */
4116 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
4117 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
4118 if (!ha->ct_sns)
4119 goto fail_free_ms_iocb;
4120 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0027,
4121 "ms_iocb=%p ct_sns=%p.\n",
4122 ha->ms_iocb, ha->ct_sns);
4123 }
4124
4125 /* Allocate memory for request ring */
4126 *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
4127 if (!*req) {
4128 ql_log_pci(ql_log_fatal, ha->pdev, 0x0028,
4129 "Failed to allocate memory for req.\n");
4130 goto fail_req;
4131 }
4132 (*req)->length = req_len;
4133 (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
4134 ((*req)->length + 1) * sizeof(request_t),
4135 &(*req)->dma, GFP_KERNEL);
4136 if (!(*req)->ring) {
4137 ql_log_pci(ql_log_fatal, ha->pdev, 0x0029,
4138 "Failed to allocate memory for req_ring.\n");
4139 goto fail_req_ring;
4140 }
4141 /* Allocate memory for response ring */
4142 *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
4143 if (!*rsp) {
4144 ql_log_pci(ql_log_fatal, ha->pdev, 0x002a,
4145 "Failed to allocate memory for rsp.\n");
4146 goto fail_rsp;
4147 }
4148 (*rsp)->hw = ha;
4149 (*rsp)->length = rsp_len;
4150 (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
4151 ((*rsp)->length + 1) * sizeof(response_t),
4152 &(*rsp)->dma, GFP_KERNEL);
4153 if (!(*rsp)->ring) {
4154 ql_log_pci(ql_log_fatal, ha->pdev, 0x002b,
4155 "Failed to allocate memory for rsp_ring.\n");
4156 goto fail_rsp_ring;
4157 }
4158 (*req)->rsp = *rsp;
4159 (*rsp)->req = *req;
4160 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002c,
4161 "req=%p req->length=%d req->ring=%p rsp=%p "
4162 "rsp->length=%d rsp->ring=%p.\n",
4163 *req, (*req)->length, (*req)->ring, *rsp, (*rsp)->length,
4164 (*rsp)->ring);
4165 /* Allocate memory for NVRAM data for vports */
4166 if (ha->nvram_npiv_size) {
4167 ha->npiv_info = kcalloc(ha->nvram_npiv_size,
4168 sizeof(struct qla_npiv_entry),
4169 GFP_KERNEL);
4170 if (!ha->npiv_info) {
4171 ql_log_pci(ql_log_fatal, ha->pdev, 0x002d,
4172 "Failed to allocate memory for npiv_info.\n");
4173 goto fail_npiv_info;
4174 }
4175 } else
4176 ha->npiv_info = NULL;
4177
4178 /* Get consistent memory allocated for EX-INIT-CB. */
4179 if (IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha) ||
4180 IS_QLA28XX(ha)) {
4181 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4182 &ha->ex_init_cb_dma);
4183 if (!ha->ex_init_cb)
4184 goto fail_ex_init_cb;
4185 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002e,
4186 "ex_init_cb=%p.\n", ha->ex_init_cb);
4187 }
4188
4189 INIT_LIST_HEAD(&ha->gbl_dsd_list);
4190
4191 /* Get consistent memory allocated for Async Port-Database. */
4192 if (!IS_FWI2_CAPABLE(ha)) {
4193 ha->async_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
4194 &ha->async_pd_dma);
4195 if (!ha->async_pd)
4196 goto fail_async_pd;
4197 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x002f,
4198 "async_pd=%p.\n", ha->async_pd);
4199 }
4200
4201 INIT_LIST_HEAD(&ha->vp_list);
4202
4203 /* Allocate memory for our loop_id bitmap */
4204 ha->loop_id_map = kcalloc(BITS_TO_LONGS(LOOPID_MAP_SIZE),
4205 sizeof(long),
4206 GFP_KERNEL);
4207 if (!ha->loop_id_map)
4208 goto fail_loop_id_map;
4209 else {
4210 qla2x00_set_reserved_loop_ids(ha);
4211 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x0123,
4212 "loop_id_map=%p.\n", ha->loop_id_map);
4213 }
4214
4215 ha->sfp_data = dma_alloc_coherent(&ha->pdev->dev,
4216 SFP_DEV_SIZE, &ha->sfp_data_dma, GFP_KERNEL);
4217 if (!ha->sfp_data) {
4218 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4219 "Unable to allocate memory for SFP read-data.\n");
4220 goto fail_sfp_data;
4221 }
4222
4223 ha->flt = dma_alloc_coherent(&ha->pdev->dev,
4224 sizeof(struct qla_flt_header) + FLT_REGIONS_SIZE, &ha->flt_dma,
4225 GFP_KERNEL);
4226 if (!ha->flt) {
4227 ql_dbg_pci(ql_dbg_init, ha->pdev, 0x011b,
4228 "Unable to allocate memory for FLT.\n");
4229 goto fail_flt_buffer;
4230 }
4231
4232 return 0;
4233
4234 fail_flt_buffer:
4235 dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE,
4236 ha->sfp_data, ha->sfp_data_dma);
4237 fail_sfp_data:
4238 kfree(ha->loop_id_map);
4239 fail_loop_id_map:
4240 dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
4241 fail_async_pd:
4242 dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
4243 fail_ex_init_cb:
4244 kfree(ha->npiv_info);
4245 fail_npiv_info:
4246 dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
4247 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
4248 (*rsp)->ring = NULL;
4249 (*rsp)->dma = 0;
4250 fail_rsp_ring:
4251 kfree(*rsp);
4252 *rsp = NULL;
4253 fail_rsp:
4254 dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
4255 sizeof(request_t), (*req)->ring, (*req)->dma);
4256 (*req)->ring = NULL;
4257 (*req)->dma = 0;
4258 fail_req_ring:
4259 kfree(*req);
4260 *req = NULL;
4261 fail_req:
4262 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
4263 ha->ct_sns, ha->ct_sns_dma);
4264 ha->ct_sns = NULL;
4265 ha->ct_sns_dma = 0;
4266 fail_free_ms_iocb:
4267 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
4268 ha->ms_iocb = NULL;
4269 ha->ms_iocb_dma = 0;
4270
4271 if (ha->sns_cmd)
4272 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
4273 ha->sns_cmd, ha->sns_cmd_dma);
4274 fail_dma_pool:
4275 if (ql2xenabledif) {
4276 struct dsd_dma *dsd, *nxt;
4277
4278 list_for_each_entry_safe(dsd, nxt, &ha->pool.unusable.head,
4279 list) {
4280 list_del(&dsd->list);
4281 dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4282 dsd->dsd_list_dma);
4283 ha->dif_bundle_dma_allocs--;
4284 kfree(dsd);
4285 ha->dif_bundle_kallocs--;
4286 ha->pool.unusable.count--;
4287 }
4288 dma_pool_destroy(ha->dif_bundl_pool);
4289 ha->dif_bundl_pool = NULL;
4290 }
4291
4292 fail_dif_bundl_dma_pool:
4293 if (IS_QLA82XX(ha) || ql2xenabledif) {
4294 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
4295 ha->fcp_cmnd_dma_pool = NULL;
4296 }
4297 fail_dl_dma_pool:
4298 if (IS_QLA82XX(ha) || ql2xenabledif) {
4299 dma_pool_destroy(ha->dl_dma_pool);
4300 ha->dl_dma_pool = NULL;
4301 }
4302 fail_s_dma_pool:
4303 dma_pool_destroy(ha->s_dma_pool);
4304 ha->s_dma_pool = NULL;
4305 fail_free_nvram:
4306 kfree(ha->nvram);
4307 ha->nvram = NULL;
4308 fail_free_ctx_mempool:
4309 mempool_destroy(ha->ctx_mempool);
4310 ha->ctx_mempool = NULL;
4311 fail_free_srb_mempool:
4312 mempool_destroy(ha->srb_mempool);
4313 ha->srb_mempool = NULL;
4314 fail_free_gid_list:
4315 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
4316 ha->gid_list,
4317 ha->gid_list_dma);
4318 ha->gid_list = NULL;
4319 ha->gid_list_dma = 0;
4320 fail_free_tgt_mem:
4321 qlt_mem_free(ha);
4322 fail_free_init_cb:
4323 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
4324 ha->init_cb_dma);
4325 ha->init_cb = NULL;
4326 ha->init_cb_dma = 0;
4327 fail:
4328 ql_log(ql_log_fatal, NULL, 0x0030,
4329 "Memory allocation failure.\n");
4330 return -ENOMEM;
4331 }
4332
4333 int
4334 qla2x00_set_exlogins_buffer(scsi_qla_host_t *vha)
4335 {
4336 int rval;
4337 uint16_t size, max_cnt, temp;
4338 struct qla_hw_data *ha = vha->hw;
4339
4340 /* Return if we don't need to alloacate any extended logins */
4341 if (!ql2xexlogins)
4342 return QLA_SUCCESS;
4343
4344 if (!IS_EXLOGIN_OFFLD_CAPABLE(ha))
4345 return QLA_SUCCESS;
4346
4347 ql_log(ql_log_info, vha, 0xd021, "EXLOGIN count: %d.\n", ql2xexlogins);
4348 max_cnt = 0;
4349 rval = qla_get_exlogin_status(vha, &size, &max_cnt);
4350 if (rval != QLA_SUCCESS) {
4351 ql_log_pci(ql_log_fatal, ha->pdev, 0xd029,
4352 "Failed to get exlogin status.\n");
4353 return rval;
4354 }
4355
4356 temp = (ql2xexlogins > max_cnt) ? max_cnt : ql2xexlogins;
4357 temp *= size;
4358
4359 if (temp != ha->exlogin_size) {
4360 qla2x00_free_exlogin_buffer(ha);
4361 ha->exlogin_size = temp;
4362
4363 ql_log(ql_log_info, vha, 0xd024,
4364 "EXLOGIN: max_logins=%d, portdb=0x%x, total=%d.\n",
4365 max_cnt, size, temp);
4366
4367 ql_log(ql_log_info, vha, 0xd025,
4368 "EXLOGIN: requested size=0x%x\n", ha->exlogin_size);
4369
4370 /* Get consistent memory for extended logins */
4371 ha->exlogin_buf = dma_alloc_coherent(&ha->pdev->dev,
4372 ha->exlogin_size, &ha->exlogin_buf_dma, GFP_KERNEL);
4373 if (!ha->exlogin_buf) {
4374 ql_log_pci(ql_log_fatal, ha->pdev, 0xd02a,
4375 "Failed to allocate memory for exlogin_buf_dma.\n");
4376 return -ENOMEM;
4377 }
4378 }
4379
4380 /* Now configure the dma buffer */
4381 rval = qla_set_exlogin_mem_cfg(vha, ha->exlogin_buf_dma);
4382 if (rval) {
4383 ql_log(ql_log_fatal, vha, 0xd033,
4384 "Setup extended login buffer ****FAILED****.\n");
4385 qla2x00_free_exlogin_buffer(ha);
4386 }
4387
4388 return rval;
4389 }
4390
4391 /*
4392 * qla2x00_free_exlogin_buffer
4393 *
4394 * Input:
4395 * ha = adapter block pointer
4396 */
4397 void
4398 qla2x00_free_exlogin_buffer(struct qla_hw_data *ha)
4399 {
4400 if (ha->exlogin_buf) {
4401 dma_free_coherent(&ha->pdev->dev, ha->exlogin_size,
4402 ha->exlogin_buf, ha->exlogin_buf_dma);
4403 ha->exlogin_buf = NULL;
4404 ha->exlogin_size = 0;
4405 }
4406 }
4407
4408 static void
4409 qla2x00_number_of_exch(scsi_qla_host_t *vha, u32 *ret_cnt, u16 max_cnt)
4410 {
4411 u32 temp;
4412 struct init_cb_81xx *icb = (struct init_cb_81xx *)&vha->hw->init_cb;
4413 *ret_cnt = FW_DEF_EXCHANGES_CNT;
4414
4415 if (max_cnt > vha->hw->max_exchg)
4416 max_cnt = vha->hw->max_exchg;
4417
4418 if (qla_ini_mode_enabled(vha)) {
4419 if (vha->ql2xiniexchg > max_cnt)
4420 vha->ql2xiniexchg = max_cnt;
4421
4422 if (vha->ql2xiniexchg > FW_DEF_EXCHANGES_CNT)
4423 *ret_cnt = vha->ql2xiniexchg;
4424
4425 } else if (qla_tgt_mode_enabled(vha)) {
4426 if (vha->ql2xexchoffld > max_cnt) {
4427 vha->ql2xexchoffld = max_cnt;
4428 icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4429 }
4430
4431 if (vha->ql2xexchoffld > FW_DEF_EXCHANGES_CNT)
4432 *ret_cnt = vha->ql2xexchoffld;
4433 } else if (qla_dual_mode_enabled(vha)) {
4434 temp = vha->ql2xiniexchg + vha->ql2xexchoffld;
4435 if (temp > max_cnt) {
4436 vha->ql2xiniexchg -= (temp - max_cnt)/2;
4437 vha->ql2xexchoffld -= (((temp - max_cnt)/2) + 1);
4438 temp = max_cnt;
4439 icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4440 }
4441
4442 if (temp > FW_DEF_EXCHANGES_CNT)
4443 *ret_cnt = temp;
4444 }
4445 }
4446
4447 int
4448 qla2x00_set_exchoffld_buffer(scsi_qla_host_t *vha)
4449 {
4450 int rval;
4451 u16 size, max_cnt;
4452 u32 actual_cnt, totsz;
4453 struct qla_hw_data *ha = vha->hw;
4454
4455 if (!ha->flags.exchoffld_enabled)
4456 return QLA_SUCCESS;
4457
4458 if (!IS_EXCHG_OFFLD_CAPABLE(ha))
4459 return QLA_SUCCESS;
4460
4461 max_cnt = 0;
4462 rval = qla_get_exchoffld_status(vha, &size, &max_cnt);
4463 if (rval != QLA_SUCCESS) {
4464 ql_log_pci(ql_log_fatal, ha->pdev, 0xd012,
4465 "Failed to get exlogin status.\n");
4466 return rval;
4467 }
4468
4469 qla2x00_number_of_exch(vha, &actual_cnt, max_cnt);
4470 ql_log(ql_log_info, vha, 0xd014,
4471 "Actual exchange offload count: %d.\n", actual_cnt);
4472
4473 totsz = actual_cnt * size;
4474
4475 if (totsz != ha->exchoffld_size) {
4476 qla2x00_free_exchoffld_buffer(ha);
4477 if (actual_cnt <= FW_DEF_EXCHANGES_CNT) {
4478 ha->exchoffld_size = 0;
4479 ha->flags.exchoffld_enabled = 0;
4480 return QLA_SUCCESS;
4481 }
4482
4483 ha->exchoffld_size = totsz;
4484
4485 ql_log(ql_log_info, vha, 0xd016,
4486 "Exchange offload: max_count=%d, actual count=%d entry sz=0x%x, total sz=0x%x\n",
4487 max_cnt, actual_cnt, size, totsz);
4488
4489 ql_log(ql_log_info, vha, 0xd017,
4490 "Exchange Buffers requested size = 0x%x\n",
4491 ha->exchoffld_size);
4492
4493 /* Get consistent memory for extended logins */
4494 ha->exchoffld_buf = dma_alloc_coherent(&ha->pdev->dev,
4495 ha->exchoffld_size, &ha->exchoffld_buf_dma, GFP_KERNEL);
4496 if (!ha->exchoffld_buf) {
4497 ql_log_pci(ql_log_fatal, ha->pdev, 0xd013,
4498 "Failed to allocate memory for Exchange Offload.\n");
4499
4500 if (ha->max_exchg >
4501 (FW_DEF_EXCHANGES_CNT + REDUCE_EXCHANGES_CNT)) {
4502 ha->max_exchg -= REDUCE_EXCHANGES_CNT;
4503 } else if (ha->max_exchg >
4504 (FW_DEF_EXCHANGES_CNT + 512)) {
4505 ha->max_exchg -= 512;
4506 } else {
4507 ha->flags.exchoffld_enabled = 0;
4508 ql_log_pci(ql_log_fatal, ha->pdev, 0xd013,
4509 "Disabling Exchange offload due to lack of memory\n");
4510 }
4511 ha->exchoffld_size = 0;
4512
4513 return -ENOMEM;
4514 }
4515 } else if (!ha->exchoffld_buf || (actual_cnt <= FW_DEF_EXCHANGES_CNT)) {
4516 /* pathological case */
4517 qla2x00_free_exchoffld_buffer(ha);
4518 ha->exchoffld_size = 0;
4519 ha->flags.exchoffld_enabled = 0;
4520 ql_log(ql_log_info, vha, 0xd016,
4521 "Exchange offload not enable: offld size=%d, actual count=%d entry sz=0x%x, total sz=0x%x.\n",
4522 ha->exchoffld_size, actual_cnt, size, totsz);
4523 return 0;
4524 }
4525
4526 /* Now configure the dma buffer */
4527 rval = qla_set_exchoffld_mem_cfg(vha);
4528 if (rval) {
4529 ql_log(ql_log_fatal, vha, 0xd02e,
4530 "Setup exchange offload buffer ****FAILED****.\n");
4531 qla2x00_free_exchoffld_buffer(ha);
4532 } else {
4533 /* re-adjust number of target exchange */
4534 struct init_cb_81xx *icb = (struct init_cb_81xx *)ha->init_cb;
4535
4536 if (qla_ini_mode_enabled(vha))
4537 icb->exchange_count = 0;
4538 else
4539 icb->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
4540 }
4541
4542 return rval;
4543 }
4544
4545 /*
4546 * qla2x00_free_exchoffld_buffer
4547 *
4548 * Input:
4549 * ha = adapter block pointer
4550 */
4551 void
4552 qla2x00_free_exchoffld_buffer(struct qla_hw_data *ha)
4553 {
4554 if (ha->exchoffld_buf) {
4555 dma_free_coherent(&ha->pdev->dev, ha->exchoffld_size,
4556 ha->exchoffld_buf, ha->exchoffld_buf_dma);
4557 ha->exchoffld_buf = NULL;
4558 ha->exchoffld_size = 0;
4559 }
4560 }
4561
4562 /*
4563 * qla2x00_free_fw_dump
4564 * Frees fw dump stuff.
4565 *
4566 * Input:
4567 * ha = adapter block pointer
4568 */
4569 static void
4570 qla2x00_free_fw_dump(struct qla_hw_data *ha)
4571 {
4572 struct fwdt *fwdt = ha->fwdt;
4573 uint j;
4574
4575 if (ha->fce)
4576 dma_free_coherent(&ha->pdev->dev,
4577 FCE_SIZE, ha->fce, ha->fce_dma);
4578
4579 if (ha->eft)
4580 dma_free_coherent(&ha->pdev->dev,
4581 EFT_SIZE, ha->eft, ha->eft_dma);
4582
4583 if (ha->fw_dump)
4584 vfree(ha->fw_dump);
4585
4586 ha->fce = NULL;
4587 ha->fce_dma = 0;
4588 ha->eft = NULL;
4589 ha->eft_dma = 0;
4590 ha->fw_dumped = 0;
4591 ha->fw_dump_cap_flags = 0;
4592 ha->fw_dump_reading = 0;
4593 ha->fw_dump = NULL;
4594 ha->fw_dump_len = 0;
4595
4596 for (j = 0; j < 2; j++, fwdt++) {
4597 if (fwdt->template)
4598 vfree(fwdt->template);
4599 fwdt->template = NULL;
4600 fwdt->length = 0;
4601 }
4602 }
4603
4604 /*
4605 * qla2x00_mem_free
4606 * Frees all adapter allocated memory.
4607 *
4608 * Input:
4609 * ha = adapter block pointer.
4610 */
4611 static void
4612 qla2x00_mem_free(struct qla_hw_data *ha)
4613 {
4614 qla2x00_free_fw_dump(ha);
4615
4616 if (ha->mctp_dump)
4617 dma_free_coherent(&ha->pdev->dev, MCTP_DUMP_SIZE, ha->mctp_dump,
4618 ha->mctp_dump_dma);
4619 ha->mctp_dump = NULL;
4620
4621 mempool_destroy(ha->srb_mempool);
4622 ha->srb_mempool = NULL;
4623
4624 if (ha->dcbx_tlv)
4625 dma_free_coherent(&ha->pdev->dev, DCBX_TLV_DATA_SIZE,
4626 ha->dcbx_tlv, ha->dcbx_tlv_dma);
4627 ha->dcbx_tlv = NULL;
4628
4629 if (ha->xgmac_data)
4630 dma_free_coherent(&ha->pdev->dev, XGMAC_DATA_SIZE,
4631 ha->xgmac_data, ha->xgmac_data_dma);
4632 ha->xgmac_data = NULL;
4633
4634 if (ha->sns_cmd)
4635 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
4636 ha->sns_cmd, ha->sns_cmd_dma);
4637 ha->sns_cmd = NULL;
4638 ha->sns_cmd_dma = 0;
4639
4640 if (ha->ct_sns)
4641 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
4642 ha->ct_sns, ha->ct_sns_dma);
4643 ha->ct_sns = NULL;
4644 ha->ct_sns_dma = 0;
4645
4646 if (ha->sfp_data)
4647 dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE, ha->sfp_data,
4648 ha->sfp_data_dma);
4649 ha->sfp_data = NULL;
4650
4651 if (ha->flt)
4652 dma_free_coherent(&ha->pdev->dev, SFP_DEV_SIZE,
4653 ha->flt, ha->flt_dma);
4654 ha->flt = NULL;
4655 ha->flt_dma = 0;
4656
4657 if (ha->ms_iocb)
4658 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
4659 ha->ms_iocb = NULL;
4660 ha->ms_iocb_dma = 0;
4661
4662 if (ha->ex_init_cb)
4663 dma_pool_free(ha->s_dma_pool,
4664 ha->ex_init_cb, ha->ex_init_cb_dma);
4665 ha->ex_init_cb = NULL;
4666 ha->ex_init_cb_dma = 0;
4667
4668 if (ha->async_pd)
4669 dma_pool_free(ha->s_dma_pool, ha->async_pd, ha->async_pd_dma);
4670 ha->async_pd = NULL;
4671 ha->async_pd_dma = 0;
4672
4673 dma_pool_destroy(ha->s_dma_pool);
4674 ha->s_dma_pool = NULL;
4675
4676 if (ha->gid_list)
4677 dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
4678 ha->gid_list, ha->gid_list_dma);
4679 ha->gid_list = NULL;
4680 ha->gid_list_dma = 0;
4681
4682 if (IS_QLA82XX(ha)) {
4683 if (!list_empty(&ha->gbl_dsd_list)) {
4684 struct dsd_dma *dsd_ptr, *tdsd_ptr;
4685
4686 /* clean up allocated prev pool */
4687 list_for_each_entry_safe(dsd_ptr,
4688 tdsd_ptr, &ha->gbl_dsd_list, list) {
4689 dma_pool_free(ha->dl_dma_pool,
4690 dsd_ptr->dsd_addr, dsd_ptr->dsd_list_dma);
4691 list_del(&dsd_ptr->list);
4692 kfree(dsd_ptr);
4693 }
4694 }
4695 }
4696
4697 dma_pool_destroy(ha->dl_dma_pool);
4698 ha->dl_dma_pool = NULL;
4699
4700 dma_pool_destroy(ha->fcp_cmnd_dma_pool);
4701 ha->fcp_cmnd_dma_pool = NULL;
4702
4703 mempool_destroy(ha->ctx_mempool);
4704 ha->ctx_mempool = NULL;
4705
4706 if (ql2xenabledif && ha->dif_bundl_pool) {
4707 struct dsd_dma *dsd, *nxt;
4708
4709 list_for_each_entry_safe(dsd, nxt, &ha->pool.unusable.head,
4710 list) {
4711 list_del(&dsd->list);
4712 dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4713 dsd->dsd_list_dma);
4714 ha->dif_bundle_dma_allocs--;
4715 kfree(dsd);
4716 ha->dif_bundle_kallocs--;
4717 ha->pool.unusable.count--;
4718 }
4719 list_for_each_entry_safe(dsd, nxt, &ha->pool.good.head, list) {
4720 list_del(&dsd->list);
4721 dma_pool_free(ha->dif_bundl_pool, dsd->dsd_addr,
4722 dsd->dsd_list_dma);
4723 ha->dif_bundle_dma_allocs--;
4724 kfree(dsd);
4725 ha->dif_bundle_kallocs--;
4726 }
4727 }
4728
4729 dma_pool_destroy(ha->dif_bundl_pool);
4730 ha->dif_bundl_pool = NULL;
4731
4732 qlt_mem_free(ha);
4733
4734 if (ha->init_cb)
4735 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
4736 ha->init_cb, ha->init_cb_dma);
4737 ha->init_cb = NULL;
4738 ha->init_cb_dma = 0;
4739
4740 vfree(ha->optrom_buffer);
4741 ha->optrom_buffer = NULL;
4742 kfree(ha->nvram);
4743 ha->nvram = NULL;
4744 kfree(ha->npiv_info);
4745 ha->npiv_info = NULL;
4746 kfree(ha->swl);
4747 ha->swl = NULL;
4748 kfree(ha->loop_id_map);
4749 ha->loop_id_map = NULL;
4750 }
4751
4752 struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
4753 struct qla_hw_data *ha)
4754 {
4755 struct Scsi_Host *host;
4756 struct scsi_qla_host *vha = NULL;
4757
4758 host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
4759 if (!host) {
4760 ql_log_pci(ql_log_fatal, ha->pdev, 0x0107,
4761 "Failed to allocate host from the scsi layer, aborting.\n");
4762 return NULL;
4763 }
4764
4765 /* Clear our data area */
4766 vha = shost_priv(host);
4767 memset(vha, 0, sizeof(scsi_qla_host_t));
4768
4769 vha->host = host;
4770 vha->host_no = host->host_no;
4771 vha->hw = ha;
4772
4773 vha->qlini_mode = ql2x_ini_mode;
4774 vha->ql2xexchoffld = ql2xexchoffld;
4775 vha->ql2xiniexchg = ql2xiniexchg;
4776
4777 INIT_LIST_HEAD(&vha->vp_fcports);
4778 INIT_LIST_HEAD(&vha->work_list);
4779 INIT_LIST_HEAD(&vha->list);
4780 INIT_LIST_HEAD(&vha->qla_cmd_list);
4781 INIT_LIST_HEAD(&vha->qla_sess_op_cmd_list);
4782 INIT_LIST_HEAD(&vha->logo_list);
4783 INIT_LIST_HEAD(&vha->plogi_ack_list);
4784 INIT_LIST_HEAD(&vha->qp_list);
4785 INIT_LIST_HEAD(&vha->gnl.fcports);
4786 INIT_LIST_HEAD(&vha->gpnid_list);
4787 INIT_WORK(&vha->iocb_work, qla2x00_iocb_work_fn);
4788
4789 spin_lock_init(&vha->work_lock);
4790 spin_lock_init(&vha->cmd_list_lock);
4791 init_waitqueue_head(&vha->fcport_waitQ);
4792 init_waitqueue_head(&vha->vref_waitq);
4793
4794 vha->gnl.size = sizeof(struct get_name_list_extended) *
4795 (ha->max_loop_id + 1);
4796 vha->gnl.l = dma_alloc_coherent(&ha->pdev->dev,
4797 vha->gnl.size, &vha->gnl.ldma, GFP_KERNEL);
4798 if (!vha->gnl.l) {
4799 ql_log(ql_log_fatal, vha, 0xd04a,
4800 "Alloc failed for name list.\n");
4801 scsi_host_put(vha->host);
4802 return NULL;
4803 }
4804
4805 /* todo: what about ext login? */
4806 vha->scan.size = ha->max_fibre_devices * sizeof(struct fab_scan_rp);
4807 vha->scan.l = vmalloc(vha->scan.size);
4808 if (!vha->scan.l) {
4809 ql_log(ql_log_fatal, vha, 0xd04a,
4810 "Alloc failed for scan database.\n");
4811 dma_free_coherent(&ha->pdev->dev, vha->gnl.size,
4812 vha->gnl.l, vha->gnl.ldma);
4813 scsi_host_put(vha->host);
4814 return NULL;
4815 }
4816 INIT_DELAYED_WORK(&vha->scan.scan_work, qla_scan_work_fn);
4817
4818 sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
4819 ql_dbg(ql_dbg_init, vha, 0x0041,
4820 "Allocated the host=%p hw=%p vha=%p dev_name=%s",
4821 vha->host, vha->hw, vha,
4822 dev_name(&(ha->pdev->dev)));
4823
4824 return vha;
4825 }
4826
4827 struct qla_work_evt *
4828 qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type)
4829 {
4830 struct qla_work_evt *e;
4831 uint8_t bail;
4832
4833 QLA_VHA_MARK_BUSY(vha, bail);
4834 if (bail)
4835 return NULL;
4836
4837 e = kzalloc(sizeof(struct qla_work_evt), GFP_ATOMIC);
4838 if (!e) {
4839 QLA_VHA_MARK_NOT_BUSY(vha);
4840 return NULL;
4841 }
4842
4843 INIT_LIST_HEAD(&e->list);
4844 e->type = type;
4845 e->flags = QLA_EVT_FLAG_FREE;
4846 return e;
4847 }
4848
4849 int
4850 qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
4851 {
4852 unsigned long flags;
4853 bool q = false;
4854
4855 spin_lock_irqsave(&vha->work_lock, flags);
4856 list_add_tail(&e->list, &vha->work_list);
4857
4858 if (!test_and_set_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags))
4859 q = true;
4860
4861 spin_unlock_irqrestore(&vha->work_lock, flags);
4862
4863 if (q)
4864 queue_work(vha->hw->wq, &vha->iocb_work);
4865
4866 return QLA_SUCCESS;
4867 }
4868
4869 int
4870 qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
4871 u32 data)
4872 {
4873 struct qla_work_evt *e;
4874
4875 e = qla2x00_alloc_work(vha, QLA_EVT_AEN);
4876 if (!e)
4877 return QLA_FUNCTION_FAILED;
4878
4879 e->u.aen.code = code;
4880 e->u.aen.data = data;
4881 return qla2x00_post_work(vha, e);
4882 }
4883
4884 int
4885 qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
4886 {
4887 struct qla_work_evt *e;
4888
4889 e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK);
4890 if (!e)
4891 return QLA_FUNCTION_FAILED;
4892
4893 memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
4894 return qla2x00_post_work(vha, e);
4895 }
4896
4897 #define qla2x00_post_async_work(name, type) \
4898 int qla2x00_post_async_##name##_work( \
4899 struct scsi_qla_host *vha, \
4900 fc_port_t *fcport, uint16_t *data) \
4901 { \
4902 struct qla_work_evt *e; \
4903 \
4904 e = qla2x00_alloc_work(vha, type); \
4905 if (!e) \
4906 return QLA_FUNCTION_FAILED; \
4907 \
4908 e->u.logio.fcport = fcport; \
4909 if (data) { \
4910 e->u.logio.data[0] = data[0]; \
4911 e->u.logio.data[1] = data[1]; \
4912 } \
4913 fcport->flags |= FCF_ASYNC_ACTIVE; \
4914 return qla2x00_post_work(vha, e); \
4915 }
4916
4917 qla2x00_post_async_work(login, QLA_EVT_ASYNC_LOGIN);
4918 qla2x00_post_async_work(logout, QLA_EVT_ASYNC_LOGOUT);
4919 qla2x00_post_async_work(logout_done, QLA_EVT_ASYNC_LOGOUT_DONE);
4920 qla2x00_post_async_work(adisc, QLA_EVT_ASYNC_ADISC);
4921 qla2x00_post_async_work(prlo, QLA_EVT_ASYNC_PRLO);
4922 qla2x00_post_async_work(prlo_done, QLA_EVT_ASYNC_PRLO_DONE);
4923
4924 int
4925 qla2x00_post_uevent_work(struct scsi_qla_host *vha, u32 code)
4926 {
4927 struct qla_work_evt *e;
4928
4929 e = qla2x00_alloc_work(vha, QLA_EVT_UEVENT);
4930 if (!e)
4931 return QLA_FUNCTION_FAILED;
4932
4933 e->u.uevent.code = code;
4934 return qla2x00_post_work(vha, e);
4935 }
4936
4937 static void
4938 qla2x00_uevent_emit(struct scsi_qla_host *vha, u32 code)
4939 {
4940 char event_string[40];
4941 char *envp[] = { event_string, NULL };
4942
4943 switch (code) {
4944 case QLA_UEVENT_CODE_FW_DUMP:
4945 snprintf(event_string, sizeof(event_string), "FW_DUMP=%ld",
4946 vha->host_no);
4947 break;
4948 default:
4949 /* do nothing */
4950 break;
4951 }
4952 kobject_uevent_env(&vha->hw->pdev->dev.kobj, KOBJ_CHANGE, envp);
4953 }
4954
4955 int
4956 qlafx00_post_aenfx_work(struct scsi_qla_host *vha, uint32_t evtcode,
4957 uint32_t *data, int cnt)
4958 {
4959 struct qla_work_evt *e;
4960
4961 e = qla2x00_alloc_work(vha, QLA_EVT_AENFX);
4962 if (!e)
4963 return QLA_FUNCTION_FAILED;
4964
4965 e->u.aenfx.evtcode = evtcode;
4966 e->u.aenfx.count = cnt;
4967 memcpy(e->u.aenfx.mbx, data, sizeof(*data) * cnt);
4968 return qla2x00_post_work(vha, e);
4969 }
4970
4971 void qla24xx_sched_upd_fcport(fc_port_t *fcport)
4972 {
4973 unsigned long flags;
4974
4975 if (IS_SW_RESV_ADDR(fcport->d_id))
4976 return;
4977
4978 spin_lock_irqsave(&fcport->vha->work_lock, flags);
4979 if (fcport->disc_state == DSC_UPD_FCPORT) {
4980 spin_unlock_irqrestore(&fcport->vha->work_lock, flags);
4981 return;
4982 }
4983 fcport->jiffies_at_registration = jiffies;
4984 fcport->sec_since_registration = 0;
4985 fcport->next_disc_state = DSC_DELETED;
4986 fcport->disc_state = DSC_UPD_FCPORT;
4987 spin_unlock_irqrestore(&fcport->vha->work_lock, flags);
4988
4989 queue_work(system_unbound_wq, &fcport->reg_work);
4990 }
4991
4992 static
4993 void qla24xx_create_new_sess(struct scsi_qla_host *vha, struct qla_work_evt *e)
4994 {
4995 unsigned long flags;
4996 fc_port_t *fcport = NULL, *tfcp;
4997 struct qlt_plogi_ack_t *pla =
4998 (struct qlt_plogi_ack_t *)e->u.new_sess.pla;
4999 uint8_t free_fcport = 0;
5000
5001 ql_dbg(ql_dbg_disc, vha, 0xffff,
5002 "%s %d %8phC enter\n",
5003 __func__, __LINE__, e->u.new_sess.port_name);
5004
5005 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5006 fcport = qla2x00_find_fcport_by_wwpn(vha, e->u.new_sess.port_name, 1);
5007 if (fcport) {
5008 fcport->d_id = e->u.new_sess.id;
5009 if (pla) {
5010 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
5011 memcpy(fcport->node_name,
5012 pla->iocb.u.isp24.u.plogi.node_name,
5013 WWN_SIZE);
5014 qlt_plogi_ack_link(vha, pla, fcport, QLT_PLOGI_LINK_SAME_WWN);
5015 /* we took an extra ref_count to prevent PLOGI ACK when
5016 * fcport/sess has not been created.
5017 */
5018 pla->ref_count--;
5019 }
5020 } else {
5021 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5022 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5023 if (fcport) {
5024 fcport->d_id = e->u.new_sess.id;
5025 fcport->flags |= FCF_FABRIC_DEVICE;
5026 fcport->fw_login_state = DSC_LS_PLOGI_PEND;
5027 if (e->u.new_sess.fc4_type == FS_FC4TYPE_FCP)
5028 fcport->fc4_type = FC4_TYPE_FCP_SCSI;
5029
5030 if (e->u.new_sess.fc4_type == FS_FC4TYPE_NVME) {
5031 fcport->fc4_type = FC4_TYPE_OTHER;
5032 fcport->fc4f_nvme = FC4_TYPE_NVME;
5033 }
5034
5035 memcpy(fcport->port_name, e->u.new_sess.port_name,
5036 WWN_SIZE);
5037 } else {
5038 ql_dbg(ql_dbg_disc, vha, 0xffff,
5039 "%s %8phC mem alloc fail.\n",
5040 __func__, e->u.new_sess.port_name);
5041
5042 if (pla) {
5043 list_del(&pla->list);
5044 kmem_cache_free(qla_tgt_plogi_cachep, pla);
5045 }
5046 return;
5047 }
5048
5049 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5050 /* search again to make sure no one else got ahead */
5051 tfcp = qla2x00_find_fcport_by_wwpn(vha,
5052 e->u.new_sess.port_name, 1);
5053 if (tfcp) {
5054 /* should rarily happen */
5055 ql_dbg(ql_dbg_disc, vha, 0xffff,
5056 "%s %8phC found existing fcport b4 add. DS %d LS %d\n",
5057 __func__, tfcp->port_name, tfcp->disc_state,
5058 tfcp->fw_login_state);
5059
5060 free_fcport = 1;
5061 } else {
5062 list_add_tail(&fcport->list, &vha->vp_fcports);
5063
5064 }
5065 if (pla) {
5066 qlt_plogi_ack_link(vha, pla, fcport,
5067 QLT_PLOGI_LINK_SAME_WWN);
5068 pla->ref_count--;
5069 }
5070 }
5071 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5072
5073 if (fcport) {
5074 fcport->id_changed = 1;
5075 fcport->scan_state = QLA_FCPORT_FOUND;
5076 fcport->chip_reset = vha->hw->base_qpair->chip_reset;
5077 memcpy(fcport->node_name, e->u.new_sess.node_name, WWN_SIZE);
5078
5079 if (pla) {
5080 if (pla->iocb.u.isp24.status_subcode == ELS_PRLI) {
5081 u16 wd3_lo;
5082
5083 fcport->fw_login_state = DSC_LS_PRLI_PEND;
5084 fcport->local = 0;
5085 fcport->loop_id =
5086 le16_to_cpu(
5087 pla->iocb.u.isp24.nport_handle);
5088 fcport->fw_login_state = DSC_LS_PRLI_PEND;
5089 wd3_lo =
5090 le16_to_cpu(
5091 pla->iocb.u.isp24.u.prli.wd3_lo);
5092
5093 if (wd3_lo & BIT_7)
5094 fcport->conf_compl_supported = 1;
5095
5096 if ((wd3_lo & BIT_4) == 0)
5097 fcport->port_type = FCT_INITIATOR;
5098 else
5099 fcport->port_type = FCT_TARGET;
5100 }
5101 qlt_plogi_ack_unref(vha, pla);
5102 } else {
5103 fc_port_t *dfcp = NULL;
5104
5105 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5106 tfcp = qla2x00_find_fcport_by_nportid(vha,
5107 &e->u.new_sess.id, 1);
5108 if (tfcp && (tfcp != fcport)) {
5109 /*
5110 * We have a conflict fcport with same NportID.
5111 */
5112 ql_dbg(ql_dbg_disc, vha, 0xffff,
5113 "%s %8phC found conflict b4 add. DS %d LS %d\n",
5114 __func__, tfcp->port_name, tfcp->disc_state,
5115 tfcp->fw_login_state);
5116
5117 switch (tfcp->disc_state) {
5118 case DSC_DELETED:
5119 break;
5120 case DSC_DELETE_PEND:
5121 fcport->login_pause = 1;
5122 tfcp->conflict = fcport;
5123 break;
5124 default:
5125 fcport->login_pause = 1;
5126 tfcp->conflict = fcport;
5127 dfcp = tfcp;
5128 break;
5129 }
5130 }
5131 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5132 if (dfcp)
5133 qlt_schedule_sess_for_deletion(tfcp);
5134
5135
5136 if (N2N_TOPO(vha->hw))
5137 fcport->flags &= ~FCF_FABRIC_DEVICE;
5138
5139 if (N2N_TOPO(vha->hw)) {
5140 if (vha->flags.nvme_enabled) {
5141 fcport->fc4f_nvme = 1;
5142 fcport->n2n_flag = 1;
5143 }
5144 fcport->fw_login_state = 0;
5145 /*
5146 * wait link init done before sending login
5147 */
5148 } else {
5149 qla24xx_fcport_handle_login(vha, fcport);
5150 }
5151 }
5152 }
5153
5154 if (free_fcport) {
5155 qla2x00_free_fcport(fcport);
5156 if (pla) {
5157 list_del(&pla->list);
5158 kmem_cache_free(qla_tgt_plogi_cachep, pla);
5159 }
5160 }
5161 }
5162
5163 static void qla_sp_retry(struct scsi_qla_host *vha, struct qla_work_evt *e)
5164 {
5165 struct srb *sp = e->u.iosb.sp;
5166 int rval;
5167
5168 rval = qla2x00_start_sp(sp);
5169 if (rval != QLA_SUCCESS) {
5170 ql_dbg(ql_dbg_disc, vha, 0x2043,
5171 "%s: %s: Re-issue IOCB failed (%d).\n",
5172 __func__, sp->name, rval);
5173 qla24xx_sp_unmap(vha, sp);
5174 }
5175 }
5176
5177 void
5178 qla2x00_do_work(struct scsi_qla_host *vha)
5179 {
5180 struct qla_work_evt *e, *tmp;
5181 unsigned long flags;
5182 LIST_HEAD(work);
5183 int rc;
5184
5185 spin_lock_irqsave(&vha->work_lock, flags);
5186 list_splice_init(&vha->work_list, &work);
5187 spin_unlock_irqrestore(&vha->work_lock, flags);
5188
5189 list_for_each_entry_safe(e, tmp, &work, list) {
5190 rc = QLA_SUCCESS;
5191 switch (e->type) {
5192 case QLA_EVT_AEN:
5193 fc_host_post_event(vha->host, fc_get_event_number(),
5194 e->u.aen.code, e->u.aen.data);
5195 break;
5196 case QLA_EVT_IDC_ACK:
5197 qla81xx_idc_ack(vha, e->u.idc_ack.mb);
5198 break;
5199 case QLA_EVT_ASYNC_LOGIN:
5200 qla2x00_async_login(vha, e->u.logio.fcport,
5201 e->u.logio.data);
5202 break;
5203 case QLA_EVT_ASYNC_LOGOUT:
5204 rc = qla2x00_async_logout(vha, e->u.logio.fcport);
5205 break;
5206 case QLA_EVT_ASYNC_LOGOUT_DONE:
5207 qla2x00_async_logout_done(vha, e->u.logio.fcport,
5208 e->u.logio.data);
5209 break;
5210 case QLA_EVT_ASYNC_ADISC:
5211 qla2x00_async_adisc(vha, e->u.logio.fcport,
5212 e->u.logio.data);
5213 break;
5214 case QLA_EVT_UEVENT:
5215 qla2x00_uevent_emit(vha, e->u.uevent.code);
5216 break;
5217 case QLA_EVT_AENFX:
5218 qlafx00_process_aen(vha, e);
5219 break;
5220 case QLA_EVT_GPNID:
5221 qla24xx_async_gpnid(vha, &e->u.gpnid.id);
5222 break;
5223 case QLA_EVT_UNMAP:
5224 qla24xx_sp_unmap(vha, e->u.iosb.sp);
5225 break;
5226 case QLA_EVT_RELOGIN:
5227 qla2x00_relogin(vha);
5228 break;
5229 case QLA_EVT_NEW_SESS:
5230 qla24xx_create_new_sess(vha, e);
5231 break;
5232 case QLA_EVT_GPDB:
5233 qla24xx_async_gpdb(vha, e->u.fcport.fcport,
5234 e->u.fcport.opt);
5235 break;
5236 case QLA_EVT_PRLI:
5237 qla24xx_async_prli(vha, e->u.fcport.fcport);
5238 break;
5239 case QLA_EVT_GPSC:
5240 qla24xx_async_gpsc(vha, e->u.fcport.fcport);
5241 break;
5242 case QLA_EVT_GNL:
5243 qla24xx_async_gnl(vha, e->u.fcport.fcport);
5244 break;
5245 case QLA_EVT_NACK:
5246 qla24xx_do_nack_work(vha, e);
5247 break;
5248 case QLA_EVT_ASYNC_PRLO:
5249 rc = qla2x00_async_prlo(vha, e->u.logio.fcport);
5250 break;
5251 case QLA_EVT_ASYNC_PRLO_DONE:
5252 qla2x00_async_prlo_done(vha, e->u.logio.fcport,
5253 e->u.logio.data);
5254 break;
5255 case QLA_EVT_GPNFT:
5256 qla24xx_async_gpnft(vha, e->u.gpnft.fc4_type,
5257 e->u.gpnft.sp);
5258 break;
5259 case QLA_EVT_GPNFT_DONE:
5260 qla24xx_async_gpnft_done(vha, e->u.iosb.sp);
5261 break;
5262 case QLA_EVT_GNNFT_DONE:
5263 qla24xx_async_gnnft_done(vha, e->u.iosb.sp);
5264 break;
5265 case QLA_EVT_GNNID:
5266 qla24xx_async_gnnid(vha, e->u.fcport.fcport);
5267 break;
5268 case QLA_EVT_GFPNID:
5269 qla24xx_async_gfpnid(vha, e->u.fcport.fcport);
5270 break;
5271 case QLA_EVT_SP_RETRY:
5272 qla_sp_retry(vha, e);
5273 break;
5274 case QLA_EVT_IIDMA:
5275 qla_do_iidma_work(vha, e->u.fcport.fcport);
5276 break;
5277 case QLA_EVT_ELS_PLOGI:
5278 qla24xx_els_dcmd2_iocb(vha, ELS_DCMD_PLOGI,
5279 e->u.fcport.fcport, false);
5280 break;
5281 }
5282
5283 if (rc == EAGAIN) {
5284 /* put 'work' at head of 'vha->work_list' */
5285 spin_lock_irqsave(&vha->work_lock, flags);
5286 list_splice(&work, &vha->work_list);
5287 spin_unlock_irqrestore(&vha->work_lock, flags);
5288 break;
5289 }
5290 list_del_init(&e->list);
5291 if (e->flags & QLA_EVT_FLAG_FREE)
5292 kfree(e);
5293
5294 /* For each work completed decrement vha ref count */
5295 QLA_VHA_MARK_NOT_BUSY(vha);
5296 }
5297 }
5298
5299 int qla24xx_post_relogin_work(struct scsi_qla_host *vha)
5300 {
5301 struct qla_work_evt *e;
5302
5303 e = qla2x00_alloc_work(vha, QLA_EVT_RELOGIN);
5304
5305 if (!e) {
5306 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5307 return QLA_FUNCTION_FAILED;
5308 }
5309
5310 return qla2x00_post_work(vha, e);
5311 }
5312
5313 /* Relogins all the fcports of a vport
5314 * Context: dpc thread
5315 */
5316 void qla2x00_relogin(struct scsi_qla_host *vha)
5317 {
5318 fc_port_t *fcport;
5319 int status, relogin_needed = 0;
5320 struct event_arg ea;
5321
5322 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5323 /*
5324 * If the port is not ONLINE then try to login
5325 * to it if we haven't run out of retries.
5326 */
5327 if (atomic_read(&fcport->state) != FCS_ONLINE &&
5328 fcport->login_retry) {
5329 if (fcport->scan_state != QLA_FCPORT_FOUND ||
5330 fcport->disc_state == DSC_LOGIN_COMPLETE)
5331 continue;
5332
5333 if (fcport->flags & (FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE) ||
5334 fcport->disc_state == DSC_DELETE_PEND) {
5335 relogin_needed = 1;
5336 } else {
5337 if (vha->hw->current_topology != ISP_CFG_NL) {
5338 memset(&ea, 0, sizeof(ea));
5339 ea.event = FCME_RELOGIN;
5340 ea.fcport = fcport;
5341 qla2x00_fcport_event_handler(vha, &ea);
5342 } else if (vha->hw->current_topology ==
5343 ISP_CFG_NL) {
5344 fcport->login_retry--;
5345 status =
5346 qla2x00_local_device_login(vha,
5347 fcport);
5348 if (status == QLA_SUCCESS) {
5349 fcport->old_loop_id =
5350 fcport->loop_id;
5351 ql_dbg(ql_dbg_disc, vha, 0x2003,
5352 "Port login OK: logged in ID 0x%x.\n",
5353 fcport->loop_id);
5354 qla2x00_update_fcport
5355 (vha, fcport);
5356 } else if (status == 1) {
5357 set_bit(RELOGIN_NEEDED,
5358 &vha->dpc_flags);
5359 /* retry the login again */
5360 ql_dbg(ql_dbg_disc, vha, 0x2007,
5361 "Retrying %d login again loop_id 0x%x.\n",
5362 fcport->login_retry,
5363 fcport->loop_id);
5364 } else {
5365 fcport->login_retry = 0;
5366 }
5367
5368 if (fcport->login_retry == 0 &&
5369 status != QLA_SUCCESS)
5370 qla2x00_clear_loop_id(fcport);
5371 }
5372 }
5373 }
5374 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5375 break;
5376 }
5377
5378 if (relogin_needed)
5379 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5380
5381 ql_dbg(ql_dbg_disc, vha, 0x400e,
5382 "Relogin end.\n");
5383 }
5384
5385 /* Schedule work on any of the dpc-workqueues */
5386 void
5387 qla83xx_schedule_work(scsi_qla_host_t *base_vha, int work_code)
5388 {
5389 struct qla_hw_data *ha = base_vha->hw;
5390
5391 switch (work_code) {
5392 case MBA_IDC_AEN: /* 0x8200 */
5393 if (ha->dpc_lp_wq)
5394 queue_work(ha->dpc_lp_wq, &ha->idc_aen);
5395 break;
5396
5397 case QLA83XX_NIC_CORE_RESET: /* 0x1 */
5398 if (!ha->flags.nic_core_reset_hdlr_active) {
5399 if (ha->dpc_hp_wq)
5400 queue_work(ha->dpc_hp_wq, &ha->nic_core_reset);
5401 } else
5402 ql_dbg(ql_dbg_p3p, base_vha, 0xb05e,
5403 "NIC Core reset is already active. Skip "
5404 "scheduling it again.\n");
5405 break;
5406 case QLA83XX_IDC_STATE_HANDLER: /* 0x2 */
5407 if (ha->dpc_hp_wq)
5408 queue_work(ha->dpc_hp_wq, &ha->idc_state_handler);
5409 break;
5410 case QLA83XX_NIC_CORE_UNRECOVERABLE: /* 0x3 */
5411 if (ha->dpc_hp_wq)
5412 queue_work(ha->dpc_hp_wq, &ha->nic_core_unrecoverable);
5413 break;
5414 default:
5415 ql_log(ql_log_warn, base_vha, 0xb05f,
5416 "Unknown work-code=0x%x.\n", work_code);
5417 }
5418
5419 return;
5420 }
5421
5422 /* Work: Perform NIC Core Unrecoverable state handling */
5423 void
5424 qla83xx_nic_core_unrecoverable_work(struct work_struct *work)
5425 {
5426 struct qla_hw_data *ha =
5427 container_of(work, struct qla_hw_data, nic_core_unrecoverable);
5428 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5429 uint32_t dev_state = 0;
5430
5431 qla83xx_idc_lock(base_vha, 0);
5432 qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5433 qla83xx_reset_ownership(base_vha);
5434 if (ha->flags.nic_core_reset_owner) {
5435 ha->flags.nic_core_reset_owner = 0;
5436 qla83xx_wr_reg(base_vha, QLA83XX_IDC_DEV_STATE,
5437 QLA8XXX_DEV_FAILED);
5438 ql_log(ql_log_info, base_vha, 0xb060, "HW State: FAILED.\n");
5439 qla83xx_schedule_work(base_vha, QLA83XX_IDC_STATE_HANDLER);
5440 }
5441 qla83xx_idc_unlock(base_vha, 0);
5442 }
5443
5444 /* Work: Execute IDC state handler */
5445 void
5446 qla83xx_idc_state_handler_work(struct work_struct *work)
5447 {
5448 struct qla_hw_data *ha =
5449 container_of(work, struct qla_hw_data, idc_state_handler);
5450 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5451 uint32_t dev_state = 0;
5452
5453 qla83xx_idc_lock(base_vha, 0);
5454 qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5455 if (dev_state == QLA8XXX_DEV_FAILED ||
5456 dev_state == QLA8XXX_DEV_NEED_QUIESCENT)
5457 qla83xx_idc_state_handler(base_vha);
5458 qla83xx_idc_unlock(base_vha, 0);
5459 }
5460
5461 static int
5462 qla83xx_check_nic_core_fw_alive(scsi_qla_host_t *base_vha)
5463 {
5464 int rval = QLA_SUCCESS;
5465 unsigned long heart_beat_wait = jiffies + (1 * HZ);
5466 uint32_t heart_beat_counter1, heart_beat_counter2;
5467
5468 do {
5469 if (time_after(jiffies, heart_beat_wait)) {
5470 ql_dbg(ql_dbg_p3p, base_vha, 0xb07c,
5471 "Nic Core f/w is not alive.\n");
5472 rval = QLA_FUNCTION_FAILED;
5473 break;
5474 }
5475
5476 qla83xx_idc_lock(base_vha, 0);
5477 qla83xx_rd_reg(base_vha, QLA83XX_FW_HEARTBEAT,
5478 &heart_beat_counter1);
5479 qla83xx_idc_unlock(base_vha, 0);
5480 msleep(100);
5481 qla83xx_idc_lock(base_vha, 0);
5482 qla83xx_rd_reg(base_vha, QLA83XX_FW_HEARTBEAT,
5483 &heart_beat_counter2);
5484 qla83xx_idc_unlock(base_vha, 0);
5485 } while (heart_beat_counter1 == heart_beat_counter2);
5486
5487 return rval;
5488 }
5489
5490 /* Work: Perform NIC Core Reset handling */
5491 void
5492 qla83xx_nic_core_reset_work(struct work_struct *work)
5493 {
5494 struct qla_hw_data *ha =
5495 container_of(work, struct qla_hw_data, nic_core_reset);
5496 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5497 uint32_t dev_state = 0;
5498
5499 if (IS_QLA2031(ha)) {
5500 if (qla2xxx_mctp_dump(base_vha) != QLA_SUCCESS)
5501 ql_log(ql_log_warn, base_vha, 0xb081,
5502 "Failed to dump mctp\n");
5503 return;
5504 }
5505
5506 if (!ha->flags.nic_core_reset_hdlr_active) {
5507 if (qla83xx_check_nic_core_fw_alive(base_vha) == QLA_SUCCESS) {
5508 qla83xx_idc_lock(base_vha, 0);
5509 qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE,
5510 &dev_state);
5511 qla83xx_idc_unlock(base_vha, 0);
5512 if (dev_state != QLA8XXX_DEV_NEED_RESET) {
5513 ql_dbg(ql_dbg_p3p, base_vha, 0xb07a,
5514 "Nic Core f/w is alive.\n");
5515 return;
5516 }
5517 }
5518
5519 ha->flags.nic_core_reset_hdlr_active = 1;
5520 if (qla83xx_nic_core_reset(base_vha)) {
5521 /* NIC Core reset failed. */
5522 ql_dbg(ql_dbg_p3p, base_vha, 0xb061,
5523 "NIC Core reset failed.\n");
5524 }
5525 ha->flags.nic_core_reset_hdlr_active = 0;
5526 }
5527 }
5528
5529 /* Work: Handle 8200 IDC aens */
5530 void
5531 qla83xx_service_idc_aen(struct work_struct *work)
5532 {
5533 struct qla_hw_data *ha =
5534 container_of(work, struct qla_hw_data, idc_aen);
5535 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
5536 uint32_t dev_state, idc_control;
5537
5538 qla83xx_idc_lock(base_vha, 0);
5539 qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5540 qla83xx_rd_reg(base_vha, QLA83XX_IDC_CONTROL, &idc_control);
5541 qla83xx_idc_unlock(base_vha, 0);
5542 if (dev_state == QLA8XXX_DEV_NEED_RESET) {
5543 if (idc_control & QLA83XX_IDC_GRACEFUL_RESET) {
5544 ql_dbg(ql_dbg_p3p, base_vha, 0xb062,
5545 "Application requested NIC Core Reset.\n");
5546 qla83xx_schedule_work(base_vha, QLA83XX_NIC_CORE_RESET);
5547 } else if (qla83xx_check_nic_core_fw_alive(base_vha) ==
5548 QLA_SUCCESS) {
5549 ql_dbg(ql_dbg_p3p, base_vha, 0xb07b,
5550 "Other protocol driver requested NIC Core Reset.\n");
5551 qla83xx_schedule_work(base_vha, QLA83XX_NIC_CORE_RESET);
5552 }
5553 } else if (dev_state == QLA8XXX_DEV_FAILED ||
5554 dev_state == QLA8XXX_DEV_NEED_QUIESCENT) {
5555 qla83xx_schedule_work(base_vha, QLA83XX_IDC_STATE_HANDLER);
5556 }
5557 }
5558
5559 static void
5560 qla83xx_wait_logic(void)
5561 {
5562 int i;
5563
5564 /* Yield CPU */
5565 if (!in_interrupt()) {
5566 /*
5567 * Wait about 200ms before retrying again.
5568 * This controls the number of retries for single
5569 * lock operation.
5570 */
5571 msleep(100);
5572 schedule();
5573 } else {
5574 for (i = 0; i < 20; i++)
5575 cpu_relax(); /* This a nop instr on i386 */
5576 }
5577 }
5578
5579 static int
5580 qla83xx_force_lock_recovery(scsi_qla_host_t *base_vha)
5581 {
5582 int rval;
5583 uint32_t data;
5584 uint32_t idc_lck_rcvry_stage_mask = 0x3;
5585 uint32_t idc_lck_rcvry_owner_mask = 0x3c;
5586 struct qla_hw_data *ha = base_vha->hw;
5587
5588 ql_dbg(ql_dbg_p3p, base_vha, 0xb086,
5589 "Trying force recovery of the IDC lock.\n");
5590
5591 rval = qla83xx_rd_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY, &data);
5592 if (rval)
5593 return rval;
5594
5595 if ((data & idc_lck_rcvry_stage_mask) > 0) {
5596 return QLA_SUCCESS;
5597 } else {
5598 data = (IDC_LOCK_RECOVERY_STAGE1) | (ha->portnum << 2);
5599 rval = qla83xx_wr_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY,
5600 data);
5601 if (rval)
5602 return rval;
5603
5604 msleep(200);
5605
5606 rval = qla83xx_rd_reg(base_vha, QLA83XX_IDC_LOCK_RECOVERY,
5607 &data);
5608 if (rval)
5609 return rval;
5610
5611 if (((data & idc_lck_rcvry_owner_mask) >> 2) == ha->portnum) {
5612 data &= (IDC_LOCK_RECOVERY_STAGE2 |
5613 ~(idc_lck_rcvry_stage_mask));
5614 rval = qla83xx_wr_reg(base_vha,
5615 QLA83XX_IDC_LOCK_RECOVERY, data);
5616 if (rval)
5617 return rval;
5618
5619 /* Forcefully perform IDC UnLock */
5620 rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_UNLOCK,
5621 &data);
5622 if (rval)
5623 return rval;
5624 /* Clear lock-id by setting 0xff */
5625 rval = qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5626 0xff);
5627 if (rval)
5628 return rval;
5629 /* Clear lock-recovery by setting 0x0 */
5630 rval = qla83xx_wr_reg(base_vha,
5631 QLA83XX_IDC_LOCK_RECOVERY, 0x0);
5632 if (rval)
5633 return rval;
5634 } else
5635 return QLA_SUCCESS;
5636 }
5637
5638 return rval;
5639 }
5640
5641 static int
5642 qla83xx_idc_lock_recovery(scsi_qla_host_t *base_vha)
5643 {
5644 int rval = QLA_SUCCESS;
5645 uint32_t o_drv_lockid, n_drv_lockid;
5646 unsigned long lock_recovery_timeout;
5647
5648 lock_recovery_timeout = jiffies + QLA83XX_MAX_LOCK_RECOVERY_WAIT;
5649 retry_lockid:
5650 rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &o_drv_lockid);
5651 if (rval)
5652 goto exit;
5653
5654 /* MAX wait time before forcing IDC Lock recovery = 2 secs */
5655 if (time_after_eq(jiffies, lock_recovery_timeout)) {
5656 if (qla83xx_force_lock_recovery(base_vha) == QLA_SUCCESS)
5657 return QLA_SUCCESS;
5658 else
5659 return QLA_FUNCTION_FAILED;
5660 }
5661
5662 rval = qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &n_drv_lockid);
5663 if (rval)
5664 goto exit;
5665
5666 if (o_drv_lockid == n_drv_lockid) {
5667 qla83xx_wait_logic();
5668 goto retry_lockid;
5669 } else
5670 return QLA_SUCCESS;
5671
5672 exit:
5673 return rval;
5674 }
5675
5676 void
5677 qla83xx_idc_lock(scsi_qla_host_t *base_vha, uint16_t requester_id)
5678 {
5679 uint32_t data;
5680 uint32_t lock_owner;
5681 struct qla_hw_data *ha = base_vha->hw;
5682
5683 /* IDC-lock implementation using driver-lock/lock-id remote registers */
5684 retry_lock:
5685 if (qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCK, &data)
5686 == QLA_SUCCESS) {
5687 if (data) {
5688 /* Setting lock-id to our function-number */
5689 qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5690 ha->portnum);
5691 } else {
5692 qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID,
5693 &lock_owner);
5694 ql_dbg(ql_dbg_p3p, base_vha, 0xb063,
5695 "Failed to acquire IDC lock, acquired by %d, "
5696 "retrying...\n", lock_owner);
5697
5698 /* Retry/Perform IDC-Lock recovery */
5699 if (qla83xx_idc_lock_recovery(base_vha)
5700 == QLA_SUCCESS) {
5701 qla83xx_wait_logic();
5702 goto retry_lock;
5703 } else
5704 ql_log(ql_log_warn, base_vha, 0xb075,
5705 "IDC Lock recovery FAILED.\n");
5706 }
5707
5708 }
5709
5710 return;
5711 }
5712
5713 void
5714 qla83xx_idc_unlock(scsi_qla_host_t *base_vha, uint16_t requester_id)
5715 {
5716 #if 0
5717 uint16_t options = (requester_id << 15) | BIT_7;
5718 #endif
5719 uint16_t retry;
5720 uint32_t data;
5721 struct qla_hw_data *ha = base_vha->hw;
5722
5723 /* IDC-unlock implementation using driver-unlock/lock-id
5724 * remote registers
5725 */
5726 retry = 0;
5727 retry_unlock:
5728 if (qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_LOCKID, &data)
5729 == QLA_SUCCESS) {
5730 if (data == ha->portnum) {
5731 qla83xx_rd_reg(base_vha, QLA83XX_DRIVER_UNLOCK, &data);
5732 /* Clearing lock-id by setting 0xff */
5733 qla83xx_wr_reg(base_vha, QLA83XX_DRIVER_LOCKID, 0xff);
5734 } else if (retry < 10) {
5735 /* SV: XXX: IDC unlock retrying needed here? */
5736
5737 /* Retry for IDC-unlock */
5738 qla83xx_wait_logic();
5739 retry++;
5740 ql_dbg(ql_dbg_p3p, base_vha, 0xb064,
5741 "Failed to release IDC lock, retrying=%d\n", retry);
5742 goto retry_unlock;
5743 }
5744 } else if (retry < 10) {
5745 /* Retry for IDC-unlock */
5746 qla83xx_wait_logic();
5747 retry++;
5748 ql_dbg(ql_dbg_p3p, base_vha, 0xb065,
5749 "Failed to read drv-lockid, retrying=%d\n", retry);
5750 goto retry_unlock;
5751 }
5752
5753 return;
5754
5755 #if 0
5756 /* XXX: IDC-unlock implementation using access-control mbx */
5757 retry = 0;
5758 retry_unlock2:
5759 if (qla83xx_access_control(base_vha, options, 0, 0, NULL)) {
5760 if (retry < 10) {
5761 /* Retry for IDC-unlock */
5762 qla83xx_wait_logic();
5763 retry++;
5764 ql_dbg(ql_dbg_p3p, base_vha, 0xb066,
5765 "Failed to release IDC lock, retrying=%d\n", retry);
5766 goto retry_unlock2;
5767 }
5768 }
5769
5770 return;
5771 #endif
5772 }
5773
5774 int
5775 __qla83xx_set_drv_presence(scsi_qla_host_t *vha)
5776 {
5777 int rval = QLA_SUCCESS;
5778 struct qla_hw_data *ha = vha->hw;
5779 uint32_t drv_presence;
5780
5781 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5782 if (rval == QLA_SUCCESS) {
5783 drv_presence |= (1 << ha->portnum);
5784 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
5785 drv_presence);
5786 }
5787
5788 return rval;
5789 }
5790
5791 int
5792 qla83xx_set_drv_presence(scsi_qla_host_t *vha)
5793 {
5794 int rval = QLA_SUCCESS;
5795
5796 qla83xx_idc_lock(vha, 0);
5797 rval = __qla83xx_set_drv_presence(vha);
5798 qla83xx_idc_unlock(vha, 0);
5799
5800 return rval;
5801 }
5802
5803 int
5804 __qla83xx_clear_drv_presence(scsi_qla_host_t *vha)
5805 {
5806 int rval = QLA_SUCCESS;
5807 struct qla_hw_data *ha = vha->hw;
5808 uint32_t drv_presence;
5809
5810 rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5811 if (rval == QLA_SUCCESS) {
5812 drv_presence &= ~(1 << ha->portnum);
5813 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
5814 drv_presence);
5815 }
5816
5817 return rval;
5818 }
5819
5820 int
5821 qla83xx_clear_drv_presence(scsi_qla_host_t *vha)
5822 {
5823 int rval = QLA_SUCCESS;
5824
5825 qla83xx_idc_lock(vha, 0);
5826 rval = __qla83xx_clear_drv_presence(vha);
5827 qla83xx_idc_unlock(vha, 0);
5828
5829 return rval;
5830 }
5831
5832 static void
5833 qla83xx_need_reset_handler(scsi_qla_host_t *vha)
5834 {
5835 struct qla_hw_data *ha = vha->hw;
5836 uint32_t drv_ack, drv_presence;
5837 unsigned long ack_timeout;
5838
5839 /* Wait for IDC ACK from all functions (DRV-ACK == DRV-PRESENCE) */
5840 ack_timeout = jiffies + (ha->fcoe_reset_timeout * HZ);
5841 while (1) {
5842 qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
5843 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
5844 if ((drv_ack & drv_presence) == drv_presence)
5845 break;
5846
5847 if (time_after_eq(jiffies, ack_timeout)) {
5848 ql_log(ql_log_warn, vha, 0xb067,
5849 "RESET ACK TIMEOUT! drv_presence=0x%x "
5850 "drv_ack=0x%x\n", drv_presence, drv_ack);
5851 /*
5852 * The function(s) which did not ack in time are forced
5853 * to withdraw any further participation in the IDC
5854 * reset.
5855 */
5856 if (drv_ack != drv_presence)
5857 qla83xx_wr_reg(vha, QLA83XX_IDC_DRV_PRESENCE,
5858 drv_ack);
5859 break;
5860 }
5861
5862 qla83xx_idc_unlock(vha, 0);
5863 msleep(1000);
5864 qla83xx_idc_lock(vha, 0);
5865 }
5866
5867 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_COLD);
5868 ql_log(ql_log_info, vha, 0xb068, "HW State: COLD/RE-INIT.\n");
5869 }
5870
5871 static int
5872 qla83xx_device_bootstrap(scsi_qla_host_t *vha)
5873 {
5874 int rval = QLA_SUCCESS;
5875 uint32_t idc_control;
5876
5877 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_INITIALIZING);
5878 ql_log(ql_log_info, vha, 0xb069, "HW State: INITIALIZING.\n");
5879
5880 /* Clearing IDC-Control Graceful-Reset Bit before resetting f/w */
5881 __qla83xx_get_idc_control(vha, &idc_control);
5882 idc_control &= ~QLA83XX_IDC_GRACEFUL_RESET;
5883 __qla83xx_set_idc_control(vha, 0);
5884
5885 qla83xx_idc_unlock(vha, 0);
5886 rval = qla83xx_restart_nic_firmware(vha);
5887 qla83xx_idc_lock(vha, 0);
5888
5889 if (rval != QLA_SUCCESS) {
5890 ql_log(ql_log_fatal, vha, 0xb06a,
5891 "Failed to restart NIC f/w.\n");
5892 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_FAILED);
5893 ql_log(ql_log_info, vha, 0xb06b, "HW State: FAILED.\n");
5894 } else {
5895 ql_dbg(ql_dbg_p3p, vha, 0xb06c,
5896 "Success in restarting nic f/w.\n");
5897 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE, QLA8XXX_DEV_READY);
5898 ql_log(ql_log_info, vha, 0xb06d, "HW State: READY.\n");
5899 }
5900
5901 return rval;
5902 }
5903
5904 /* Assumes idc_lock always held on entry */
5905 int
5906 qla83xx_idc_state_handler(scsi_qla_host_t *base_vha)
5907 {
5908 struct qla_hw_data *ha = base_vha->hw;
5909 int rval = QLA_SUCCESS;
5910 unsigned long dev_init_timeout;
5911 uint32_t dev_state;
5912
5913 /* Wait for MAX-INIT-TIMEOUT for the device to go ready */
5914 dev_init_timeout = jiffies + (ha->fcoe_dev_init_timeout * HZ);
5915
5916 while (1) {
5917
5918 if (time_after_eq(jiffies, dev_init_timeout)) {
5919 ql_log(ql_log_warn, base_vha, 0xb06e,
5920 "Initialization TIMEOUT!\n");
5921 /* Init timeout. Disable further NIC Core
5922 * communication.
5923 */
5924 qla83xx_wr_reg(base_vha, QLA83XX_IDC_DEV_STATE,
5925 QLA8XXX_DEV_FAILED);
5926 ql_log(ql_log_info, base_vha, 0xb06f,
5927 "HW State: FAILED.\n");
5928 }
5929
5930 qla83xx_rd_reg(base_vha, QLA83XX_IDC_DEV_STATE, &dev_state);
5931 switch (dev_state) {
5932 case QLA8XXX_DEV_READY:
5933 if (ha->flags.nic_core_reset_owner)
5934 qla83xx_idc_audit(base_vha,
5935 IDC_AUDIT_COMPLETION);
5936 ha->flags.nic_core_reset_owner = 0;
5937 ql_dbg(ql_dbg_p3p, base_vha, 0xb070,
5938 "Reset_owner reset by 0x%x.\n",
5939 ha->portnum);
5940 goto exit;
5941 case QLA8XXX_DEV_COLD:
5942 if (ha->flags.nic_core_reset_owner)
5943 rval = qla83xx_device_bootstrap(base_vha);
5944 else {
5945 /* Wait for AEN to change device-state */
5946 qla83xx_idc_unlock(base_vha, 0);
5947 msleep(1000);
5948 qla83xx_idc_lock(base_vha, 0);
5949 }
5950 break;
5951 case QLA8XXX_DEV_INITIALIZING:
5952 /* Wait for AEN to change device-state */
5953 qla83xx_idc_unlock(base_vha, 0);
5954 msleep(1000);
5955 qla83xx_idc_lock(base_vha, 0);
5956 break;
5957 case QLA8XXX_DEV_NEED_RESET:
5958 if (!ql2xdontresethba && ha->flags.nic_core_reset_owner)
5959 qla83xx_need_reset_handler(base_vha);
5960 else {
5961 /* Wait for AEN to change device-state */
5962 qla83xx_idc_unlock(base_vha, 0);
5963 msleep(1000);
5964 qla83xx_idc_lock(base_vha, 0);
5965 }
5966 /* reset timeout value after need reset handler */
5967 dev_init_timeout = jiffies +
5968 (ha->fcoe_dev_init_timeout * HZ);
5969 break;
5970 case QLA8XXX_DEV_NEED_QUIESCENT:
5971 /* XXX: DEBUG for now */
5972 qla83xx_idc_unlock(base_vha, 0);
5973 msleep(1000);
5974 qla83xx_idc_lock(base_vha, 0);
5975 break;
5976 case QLA8XXX_DEV_QUIESCENT:
5977 /* XXX: DEBUG for now */
5978 if (ha->flags.quiesce_owner)
5979 goto exit;
5980
5981 qla83xx_idc_unlock(base_vha, 0);
5982 msleep(1000);
5983 qla83xx_idc_lock(base_vha, 0);
5984 dev_init_timeout = jiffies +
5985 (ha->fcoe_dev_init_timeout * HZ);
5986 break;
5987 case QLA8XXX_DEV_FAILED:
5988 if (ha->flags.nic_core_reset_owner)
5989 qla83xx_idc_audit(base_vha,
5990 IDC_AUDIT_COMPLETION);
5991 ha->flags.nic_core_reset_owner = 0;
5992 __qla83xx_clear_drv_presence(base_vha);
5993 qla83xx_idc_unlock(base_vha, 0);
5994 qla8xxx_dev_failed_handler(base_vha);
5995 rval = QLA_FUNCTION_FAILED;
5996 qla83xx_idc_lock(base_vha, 0);
5997 goto exit;
5998 case QLA8XXX_BAD_VALUE:
5999 qla83xx_idc_unlock(base_vha, 0);
6000 msleep(1000);
6001 qla83xx_idc_lock(base_vha, 0);
6002 break;
6003 default:
6004 ql_log(ql_log_warn, base_vha, 0xb071,
6005 "Unknown Device State: %x.\n", dev_state);
6006 qla83xx_idc_unlock(base_vha, 0);
6007 qla8xxx_dev_failed_handler(base_vha);
6008 rval = QLA_FUNCTION_FAILED;
6009 qla83xx_idc_lock(base_vha, 0);
6010 goto exit;
6011 }
6012 }
6013
6014 exit:
6015 return rval;
6016 }
6017
6018 void
6019 qla2x00_disable_board_on_pci_error(struct work_struct *work)
6020 {
6021 struct qla_hw_data *ha = container_of(work, struct qla_hw_data,
6022 board_disable);
6023 struct pci_dev *pdev = ha->pdev;
6024 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6025
6026 /*
6027 * if UNLOAD flag is already set, then continue unload,
6028 * where it was set first.
6029 */
6030 if (test_bit(UNLOADING, &base_vha->dpc_flags))
6031 return;
6032
6033 ql_log(ql_log_warn, base_vha, 0x015b,
6034 "Disabling adapter.\n");
6035
6036 if (!atomic_read(&pdev->enable_cnt)) {
6037 ql_log(ql_log_info, base_vha, 0xfffc,
6038 "PCI device disabled, no action req for PCI error=%lx\n",
6039 base_vha->pci_flags);
6040 return;
6041 }
6042
6043 qla2x00_wait_for_sess_deletion(base_vha);
6044
6045 set_bit(UNLOADING, &base_vha->dpc_flags);
6046
6047 qla2x00_delete_all_vps(ha, base_vha);
6048
6049 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
6050
6051 qla2x00_dfs_remove(base_vha);
6052
6053 qla84xx_put_chip(base_vha);
6054
6055 if (base_vha->timer_active)
6056 qla2x00_stop_timer(base_vha);
6057
6058 base_vha->flags.online = 0;
6059
6060 qla2x00_destroy_deferred_work(ha);
6061
6062 /*
6063 * Do not try to stop beacon blink as it will issue a mailbox
6064 * command.
6065 */
6066 qla2x00_free_sysfs_attr(base_vha, false);
6067
6068 fc_remove_host(base_vha->host);
6069
6070 scsi_remove_host(base_vha->host);
6071
6072 base_vha->flags.init_done = 0;
6073 qla25xx_delete_queues(base_vha);
6074 qla2x00_free_fcports(base_vha);
6075 qla2x00_free_irqs(base_vha);
6076 qla2x00_mem_free(ha);
6077 qla82xx_md_free(base_vha);
6078 qla2x00_free_queues(ha);
6079
6080 qla2x00_unmap_iobases(ha);
6081
6082 pci_release_selected_regions(ha->pdev, ha->bars);
6083 pci_disable_pcie_error_reporting(pdev);
6084 pci_disable_device(pdev);
6085
6086 /*
6087 * Let qla2x00_remove_one cleanup qla_hw_data on device removal.
6088 */
6089 }
6090
6091 /**************************************************************************
6092 * qla2x00_do_dpc
6093 * This kernel thread is a task that is schedule by the interrupt handler
6094 * to perform the background processing for interrupts.
6095 *
6096 * Notes:
6097 * This task always run in the context of a kernel thread. It
6098 * is kick-off by the driver's detect code and starts up
6099 * up one per adapter. It immediately goes to sleep and waits for
6100 * some fibre event. When either the interrupt handler or
6101 * the timer routine detects a event it will one of the task
6102 * bits then wake us up.
6103 **************************************************************************/
6104 static int
6105 qla2x00_do_dpc(void *data)
6106 {
6107 scsi_qla_host_t *base_vha;
6108 struct qla_hw_data *ha;
6109 uint32_t online;
6110 struct qla_qpair *qpair;
6111
6112 ha = (struct qla_hw_data *)data;
6113 base_vha = pci_get_drvdata(ha->pdev);
6114
6115 set_user_nice(current, MIN_NICE);
6116
6117 set_current_state(TASK_INTERRUPTIBLE);
6118 while (!kthread_should_stop()) {
6119 ql_dbg(ql_dbg_dpc, base_vha, 0x4000,
6120 "DPC handler sleeping.\n");
6121
6122 schedule();
6123
6124 if (!base_vha->flags.init_done || ha->flags.mbox_busy)
6125 goto end_loop;
6126
6127 if (ha->flags.eeh_busy) {
6128 ql_dbg(ql_dbg_dpc, base_vha, 0x4003,
6129 "eeh_busy=%d.\n", ha->flags.eeh_busy);
6130 goto end_loop;
6131 }
6132
6133 ha->dpc_active = 1;
6134
6135 ql_dbg(ql_dbg_dpc + ql_dbg_verbose, base_vha, 0x4001,
6136 "DPC handler waking up, dpc_flags=0x%lx.\n",
6137 base_vha->dpc_flags);
6138
6139 if (test_bit(UNLOADING, &base_vha->dpc_flags))
6140 break;
6141
6142 if (IS_P3P_TYPE(ha)) {
6143 if (IS_QLA8044(ha)) {
6144 if (test_and_clear_bit(ISP_UNRECOVERABLE,
6145 &base_vha->dpc_flags)) {
6146 qla8044_idc_lock(ha);
6147 qla8044_wr_direct(base_vha,
6148 QLA8044_CRB_DEV_STATE_INDEX,
6149 QLA8XXX_DEV_FAILED);
6150 qla8044_idc_unlock(ha);
6151 ql_log(ql_log_info, base_vha, 0x4004,
6152 "HW State: FAILED.\n");
6153 qla8044_device_state_handler(base_vha);
6154 continue;
6155 }
6156
6157 } else {
6158 if (test_and_clear_bit(ISP_UNRECOVERABLE,
6159 &base_vha->dpc_flags)) {
6160 qla82xx_idc_lock(ha);
6161 qla82xx_wr_32(ha, QLA82XX_CRB_DEV_STATE,
6162 QLA8XXX_DEV_FAILED);
6163 qla82xx_idc_unlock(ha);
6164 ql_log(ql_log_info, base_vha, 0x0151,
6165 "HW State: FAILED.\n");
6166 qla82xx_device_state_handler(base_vha);
6167 continue;
6168 }
6169 }
6170
6171 if (test_and_clear_bit(FCOE_CTX_RESET_NEEDED,
6172 &base_vha->dpc_flags)) {
6173
6174 ql_dbg(ql_dbg_dpc, base_vha, 0x4005,
6175 "FCoE context reset scheduled.\n");
6176 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
6177 &base_vha->dpc_flags))) {
6178 if (qla82xx_fcoe_ctx_reset(base_vha)) {
6179 /* FCoE-ctx reset failed.
6180 * Escalate to chip-reset
6181 */
6182 set_bit(ISP_ABORT_NEEDED,
6183 &base_vha->dpc_flags);
6184 }
6185 clear_bit(ABORT_ISP_ACTIVE,
6186 &base_vha->dpc_flags);
6187 }
6188
6189 ql_dbg(ql_dbg_dpc, base_vha, 0x4006,
6190 "FCoE context reset end.\n");
6191 }
6192 } else if (IS_QLAFX00(ha)) {
6193 if (test_and_clear_bit(ISP_UNRECOVERABLE,
6194 &base_vha->dpc_flags)) {
6195 ql_dbg(ql_dbg_dpc, base_vha, 0x4020,
6196 "Firmware Reset Recovery\n");
6197 if (qlafx00_reset_initialize(base_vha)) {
6198 /* Failed. Abort isp later. */
6199 if (!test_bit(UNLOADING,
6200 &base_vha->dpc_flags)) {
6201 set_bit(ISP_UNRECOVERABLE,
6202 &base_vha->dpc_flags);
6203 ql_dbg(ql_dbg_dpc, base_vha,
6204 0x4021,
6205 "Reset Recovery Failed\n");
6206 }
6207 }
6208 }
6209
6210 if (test_and_clear_bit(FX00_TARGET_SCAN,
6211 &base_vha->dpc_flags)) {
6212 ql_dbg(ql_dbg_dpc, base_vha, 0x4022,
6213 "ISPFx00 Target Scan scheduled\n");
6214 if (qlafx00_rescan_isp(base_vha)) {
6215 if (!test_bit(UNLOADING,
6216 &base_vha->dpc_flags))
6217 set_bit(ISP_UNRECOVERABLE,
6218 &base_vha->dpc_flags);
6219 ql_dbg(ql_dbg_dpc, base_vha, 0x401e,
6220 "ISPFx00 Target Scan Failed\n");
6221 }
6222 ql_dbg(ql_dbg_dpc, base_vha, 0x401f,
6223 "ISPFx00 Target Scan End\n");
6224 }
6225 if (test_and_clear_bit(FX00_HOST_INFO_RESEND,
6226 &base_vha->dpc_flags)) {
6227 ql_dbg(ql_dbg_dpc, base_vha, 0x4023,
6228 "ISPFx00 Host Info resend scheduled\n");
6229 qlafx00_fx_disc(base_vha,
6230 &base_vha->hw->mr.fcport,
6231 FXDISC_REG_HOST_INFO);
6232 }
6233 }
6234
6235 if (test_and_clear_bit(DETECT_SFP_CHANGE,
6236 &base_vha->dpc_flags) &&
6237 !test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) {
6238 qla24xx_detect_sfp(base_vha);
6239
6240 if (ha->flags.detected_lr_sfp !=
6241 ha->flags.using_lr_setting)
6242 set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
6243 }
6244
6245 if (test_and_clear_bit
6246 (ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
6247 !test_bit(UNLOADING, &base_vha->dpc_flags)) {
6248 bool do_reset = true;
6249
6250 switch (base_vha->qlini_mode) {
6251 case QLA2XXX_INI_MODE_ENABLED:
6252 break;
6253 case QLA2XXX_INI_MODE_DISABLED:
6254 if (!qla_tgt_mode_enabled(base_vha) &&
6255 !ha->flags.fw_started)
6256 do_reset = false;
6257 break;
6258 case QLA2XXX_INI_MODE_DUAL:
6259 if (!qla_dual_mode_enabled(base_vha) &&
6260 !ha->flags.fw_started)
6261 do_reset = false;
6262 break;
6263 default:
6264 break;
6265 }
6266
6267 if (do_reset && !(test_and_set_bit(ABORT_ISP_ACTIVE,
6268 &base_vha->dpc_flags))) {
6269 ql_dbg(ql_dbg_dpc, base_vha, 0x4007,
6270 "ISP abort scheduled.\n");
6271 if (ha->isp_ops->abort_isp(base_vha)) {
6272 /* failed. retry later */
6273 set_bit(ISP_ABORT_NEEDED,
6274 &base_vha->dpc_flags);
6275 }
6276 clear_bit(ABORT_ISP_ACTIVE,
6277 &base_vha->dpc_flags);
6278 ql_dbg(ql_dbg_dpc, base_vha, 0x4008,
6279 "ISP abort end.\n");
6280 }
6281 }
6282
6283 if (test_and_clear_bit(FCPORT_UPDATE_NEEDED,
6284 &base_vha->dpc_flags)) {
6285 qla2x00_update_fcports(base_vha);
6286 }
6287
6288 if (IS_QLAFX00(ha))
6289 goto loop_resync_check;
6290
6291 if (test_bit(ISP_QUIESCE_NEEDED, &base_vha->dpc_flags)) {
6292 ql_dbg(ql_dbg_dpc, base_vha, 0x4009,
6293 "Quiescence mode scheduled.\n");
6294 if (IS_P3P_TYPE(ha)) {
6295 if (IS_QLA82XX(ha))
6296 qla82xx_device_state_handler(base_vha);
6297 if (IS_QLA8044(ha))
6298 qla8044_device_state_handler(base_vha);
6299 clear_bit(ISP_QUIESCE_NEEDED,
6300 &base_vha->dpc_flags);
6301 if (!ha->flags.quiesce_owner) {
6302 qla2x00_perform_loop_resync(base_vha);
6303 if (IS_QLA82XX(ha)) {
6304 qla82xx_idc_lock(ha);
6305 qla82xx_clear_qsnt_ready(
6306 base_vha);
6307 qla82xx_idc_unlock(ha);
6308 } else if (IS_QLA8044(ha)) {
6309 qla8044_idc_lock(ha);
6310 qla8044_clear_qsnt_ready(
6311 base_vha);
6312 qla8044_idc_unlock(ha);
6313 }
6314 }
6315 } else {
6316 clear_bit(ISP_QUIESCE_NEEDED,
6317 &base_vha->dpc_flags);
6318 qla2x00_quiesce_io(base_vha);
6319 }
6320 ql_dbg(ql_dbg_dpc, base_vha, 0x400a,
6321 "Quiescence mode end.\n");
6322 }
6323
6324 if (test_and_clear_bit(RESET_MARKER_NEEDED,
6325 &base_vha->dpc_flags) &&
6326 (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
6327
6328 ql_dbg(ql_dbg_dpc, base_vha, 0x400b,
6329 "Reset marker scheduled.\n");
6330 qla2x00_rst_aen(base_vha);
6331 clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
6332 ql_dbg(ql_dbg_dpc, base_vha, 0x400c,
6333 "Reset marker end.\n");
6334 }
6335
6336 /* Retry each device up to login retry count */
6337 if (test_bit(RELOGIN_NEEDED, &base_vha->dpc_flags) &&
6338 !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
6339 atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
6340
6341 if (!base_vha->relogin_jif ||
6342 time_after_eq(jiffies, base_vha->relogin_jif)) {
6343 base_vha->relogin_jif = jiffies + HZ;
6344 clear_bit(RELOGIN_NEEDED, &base_vha->dpc_flags);
6345
6346 ql_dbg(ql_dbg_disc, base_vha, 0x400d,
6347 "Relogin scheduled.\n");
6348 qla24xx_post_relogin_work(base_vha);
6349 }
6350 }
6351 loop_resync_check:
6352 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
6353 &base_vha->dpc_flags)) {
6354
6355 ql_dbg(ql_dbg_dpc, base_vha, 0x400f,
6356 "Loop resync scheduled.\n");
6357
6358 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
6359 &base_vha->dpc_flags))) {
6360
6361 qla2x00_loop_resync(base_vha);
6362
6363 clear_bit(LOOP_RESYNC_ACTIVE,
6364 &base_vha->dpc_flags);
6365 }
6366
6367 ql_dbg(ql_dbg_dpc, base_vha, 0x4010,
6368 "Loop resync end.\n");
6369 }
6370
6371 if (IS_QLAFX00(ha))
6372 goto intr_on_check;
6373
6374 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
6375 atomic_read(&base_vha->loop_state) == LOOP_READY) {
6376 clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
6377 qla2xxx_flash_npiv_conf(base_vha);
6378 }
6379
6380 intr_on_check:
6381 if (!ha->interrupts_on)
6382 ha->isp_ops->enable_intrs(ha);
6383
6384 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
6385 &base_vha->dpc_flags)) {
6386 if (ha->beacon_blink_led == 1)
6387 ha->isp_ops->beacon_blink(base_vha);
6388 }
6389
6390 /* qpair online check */
6391 if (test_and_clear_bit(QPAIR_ONLINE_CHECK_NEEDED,
6392 &base_vha->dpc_flags)) {
6393 if (ha->flags.eeh_busy ||
6394 ha->flags.pci_channel_io_perm_failure)
6395 online = 0;
6396 else
6397 online = 1;
6398
6399 mutex_lock(&ha->mq_lock);
6400 list_for_each_entry(qpair, &base_vha->qp_list,
6401 qp_list_elem)
6402 qpair->online = online;
6403 mutex_unlock(&ha->mq_lock);
6404 }
6405
6406 if (test_and_clear_bit(SET_NVME_ZIO_THRESHOLD_NEEDED,
6407 &base_vha->dpc_flags)) {
6408 ql_log(ql_log_info, base_vha, 0xffffff,
6409 "nvme: SET ZIO Activity exchange threshold to %d.\n",
6410 ha->nvme_last_rptd_aen);
6411 if (qla27xx_set_zio_threshold(base_vha,
6412 ha->nvme_last_rptd_aen)) {
6413 ql_log(ql_log_info, base_vha, 0xffffff,
6414 "nvme: Unable to SET ZIO Activity exchange threshold to %d.\n",
6415 ha->nvme_last_rptd_aen);
6416 }
6417 }
6418
6419 if (test_and_clear_bit(SET_ZIO_THRESHOLD_NEEDED,
6420 &base_vha->dpc_flags)) {
6421 ql_log(ql_log_info, base_vha, 0xffffff,
6422 "SET ZIO Activity exchange threshold to %d.\n",
6423 ha->last_zio_threshold);
6424 qla27xx_set_zio_threshold(base_vha,
6425 ha->last_zio_threshold);
6426 }
6427
6428 if (!IS_QLAFX00(ha))
6429 qla2x00_do_dpc_all_vps(base_vha);
6430
6431 if (test_and_clear_bit(N2N_LINK_RESET,
6432 &base_vha->dpc_flags)) {
6433 qla2x00_lip_reset(base_vha);
6434 }
6435
6436 ha->dpc_active = 0;
6437 end_loop:
6438 set_current_state(TASK_INTERRUPTIBLE);
6439 } /* End of while(1) */
6440 __set_current_state(TASK_RUNNING);
6441
6442 ql_dbg(ql_dbg_dpc, base_vha, 0x4011,
6443 "DPC handler exiting.\n");
6444
6445 /*
6446 * Make sure that nobody tries to wake us up again.
6447 */
6448 ha->dpc_active = 0;
6449
6450 /* Cleanup any residual CTX SRBs. */
6451 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
6452
6453 return 0;
6454 }
6455
6456 void
6457 qla2xxx_wake_dpc(struct scsi_qla_host *vha)
6458 {
6459 struct qla_hw_data *ha = vha->hw;
6460 struct task_struct *t = ha->dpc_thread;
6461
6462 if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
6463 wake_up_process(t);
6464 }
6465
6466 /*
6467 * qla2x00_rst_aen
6468 * Processes asynchronous reset.
6469 *
6470 * Input:
6471 * ha = adapter block pointer.
6472 */
6473 static void
6474 qla2x00_rst_aen(scsi_qla_host_t *vha)
6475 {
6476 if (vha->flags.online && !vha->flags.reset_active &&
6477 !atomic_read(&vha->loop_down_timer) &&
6478 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
6479 do {
6480 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
6481
6482 /*
6483 * Issue marker command only when we are going to start
6484 * the I/O.
6485 */
6486 vha->marker_needed = 1;
6487 } while (!atomic_read(&vha->loop_down_timer) &&
6488 (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
6489 }
6490 }
6491
6492 /**************************************************************************
6493 * qla2x00_timer
6494 *
6495 * Description:
6496 * One second timer
6497 *
6498 * Context: Interrupt
6499 ***************************************************************************/
6500 void
6501 qla2x00_timer(struct timer_list *t)
6502 {
6503 scsi_qla_host_t *vha = from_timer(vha, t, timer);
6504 unsigned long cpu_flags = 0;
6505 int start_dpc = 0;
6506 int index;
6507 srb_t *sp;
6508 uint16_t w;
6509 struct qla_hw_data *ha = vha->hw;
6510 struct req_que *req;
6511
6512 if (ha->flags.eeh_busy) {
6513 ql_dbg(ql_dbg_timer, vha, 0x6000,
6514 "EEH = %d, restarting timer.\n",
6515 ha->flags.eeh_busy);
6516 qla2x00_restart_timer(vha, WATCH_INTERVAL);
6517 return;
6518 }
6519
6520 /*
6521 * Hardware read to raise pending EEH errors during mailbox waits. If
6522 * the read returns -1 then disable the board.
6523 */
6524 if (!pci_channel_offline(ha->pdev)) {
6525 pci_read_config_word(ha->pdev, PCI_VENDOR_ID, &w);
6526 qla2x00_check_reg16_for_disconnect(vha, w);
6527 }
6528
6529 /* Make sure qla82xx_watchdog is run only for physical port */
6530 if (!vha->vp_idx && IS_P3P_TYPE(ha)) {
6531 if (test_bit(ISP_QUIESCE_NEEDED, &vha->dpc_flags))
6532 start_dpc++;
6533 if (IS_QLA82XX(ha))
6534 qla82xx_watchdog(vha);
6535 else if (IS_QLA8044(ha))
6536 qla8044_watchdog(vha);
6537 }
6538
6539 if (!vha->vp_idx && IS_QLAFX00(ha))
6540 qlafx00_timer_routine(vha);
6541
6542 /* Loop down handler. */
6543 if (atomic_read(&vha->loop_down_timer) > 0 &&
6544 !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) &&
6545 !(test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags))
6546 && vha->flags.online) {
6547
6548 if (atomic_read(&vha->loop_down_timer) ==
6549 vha->loop_down_abort_time) {
6550
6551 ql_log(ql_log_info, vha, 0x6008,
6552 "Loop down - aborting the queues before time expires.\n");
6553
6554 if (!IS_QLA2100(ha) && vha->link_down_timeout)
6555 atomic_set(&vha->loop_state, LOOP_DEAD);
6556
6557 /*
6558 * Schedule an ISP abort to return any FCP2-device
6559 * commands.
6560 */
6561 /* NPIV - scan physical port only */
6562 if (!vha->vp_idx) {
6563 spin_lock_irqsave(&ha->hardware_lock,
6564 cpu_flags);
6565 req = ha->req_q_map[0];
6566 for (index = 1;
6567 index < req->num_outstanding_cmds;
6568 index++) {
6569 fc_port_t *sfcp;
6570
6571 sp = req->outstanding_cmds[index];
6572 if (!sp)
6573 continue;
6574 if (sp->cmd_type != TYPE_SRB)
6575 continue;
6576 if (sp->type != SRB_SCSI_CMD)
6577 continue;
6578 sfcp = sp->fcport;
6579 if (!(sfcp->flags & FCF_FCP2_DEVICE))
6580 continue;
6581
6582 if (IS_QLA82XX(ha))
6583 set_bit(FCOE_CTX_RESET_NEEDED,
6584 &vha->dpc_flags);
6585 else
6586 set_bit(ISP_ABORT_NEEDED,
6587 &vha->dpc_flags);
6588 break;
6589 }
6590 spin_unlock_irqrestore(&ha->hardware_lock,
6591 cpu_flags);
6592 }
6593 start_dpc++;
6594 }
6595
6596 /* if the loop has been down for 4 minutes, reinit adapter */
6597 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
6598 if (!(vha->device_flags & DFLG_NO_CABLE)) {
6599 ql_log(ql_log_warn, vha, 0x6009,
6600 "Loop down - aborting ISP.\n");
6601
6602 if (IS_QLA82XX(ha))
6603 set_bit(FCOE_CTX_RESET_NEEDED,
6604 &vha->dpc_flags);
6605 else
6606 set_bit(ISP_ABORT_NEEDED,
6607 &vha->dpc_flags);
6608 }
6609 }
6610 ql_dbg(ql_dbg_timer, vha, 0x600a,
6611 "Loop down - seconds remaining %d.\n",
6612 atomic_read(&vha->loop_down_timer));
6613 }
6614 /* Check if beacon LED needs to be blinked for physical host only */
6615 if (!vha->vp_idx && (ha->beacon_blink_led == 1)) {
6616 /* There is no beacon_blink function for ISP82xx */
6617 if (!IS_P3P_TYPE(ha)) {
6618 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
6619 start_dpc++;
6620 }
6621 }
6622
6623 /* Process any deferred work. */
6624 if (!list_empty(&vha->work_list)) {
6625 unsigned long flags;
6626 bool q = false;
6627
6628 spin_lock_irqsave(&vha->work_lock, flags);
6629 if (!test_and_set_bit(IOCB_WORK_ACTIVE, &vha->dpc_flags))
6630 q = true;
6631 spin_unlock_irqrestore(&vha->work_lock, flags);
6632 if (q)
6633 queue_work(vha->hw->wq, &vha->iocb_work);
6634 }
6635
6636 /*
6637 * FC-NVME
6638 * see if the active AEN count has changed from what was last reported.
6639 */
6640 if (!vha->vp_idx &&
6641 (atomic_read(&ha->nvme_active_aen_cnt) != ha->nvme_last_rptd_aen) &&
6642 ha->zio_mode == QLA_ZIO_MODE_6 &&
6643 !ha->flags.host_shutting_down) {
6644 ql_log(ql_log_info, vha, 0x3002,
6645 "nvme: Sched: Set ZIO exchange threshold to %d.\n",
6646 ha->nvme_last_rptd_aen);
6647 ha->nvme_last_rptd_aen = atomic_read(&ha->nvme_active_aen_cnt);
6648 set_bit(SET_NVME_ZIO_THRESHOLD_NEEDED, &vha->dpc_flags);
6649 start_dpc++;
6650 }
6651
6652 if (!vha->vp_idx &&
6653 (atomic_read(&ha->zio_threshold) != ha->last_zio_threshold) &&
6654 (ha->zio_mode == QLA_ZIO_MODE_6) &&
6655 (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))) {
6656 ql_log(ql_log_info, vha, 0x3002,
6657 "Sched: Set ZIO exchange threshold to %d.\n",
6658 ha->last_zio_threshold);
6659 ha->last_zio_threshold = atomic_read(&ha->zio_threshold);
6660 set_bit(SET_ZIO_THRESHOLD_NEEDED, &vha->dpc_flags);
6661 start_dpc++;
6662 }
6663
6664 /* Schedule the DPC routine if needed */
6665 if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
6666 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
6667 test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
6668 start_dpc ||
6669 test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
6670 test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
6671 test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags) ||
6672 test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags) ||
6673 test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
6674 test_bit(RELOGIN_NEEDED, &vha->dpc_flags))) {
6675 ql_dbg(ql_dbg_timer, vha, 0x600b,
6676 "isp_abort_needed=%d loop_resync_needed=%d "
6677 "fcport_update_needed=%d start_dpc=%d "
6678 "reset_marker_needed=%d",
6679 test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags),
6680 test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags),
6681 test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags),
6682 start_dpc,
6683 test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags));
6684 ql_dbg(ql_dbg_timer, vha, 0x600c,
6685 "beacon_blink_needed=%d isp_unrecoverable=%d "
6686 "fcoe_ctx_reset_needed=%d vp_dpc_needed=%d "
6687 "relogin_needed=%d.\n",
6688 test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags),
6689 test_bit(ISP_UNRECOVERABLE, &vha->dpc_flags),
6690 test_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags),
6691 test_bit(VP_DPC_NEEDED, &vha->dpc_flags),
6692 test_bit(RELOGIN_NEEDED, &vha->dpc_flags));
6693 qla2xxx_wake_dpc(vha);
6694 }
6695
6696 qla2x00_restart_timer(vha, WATCH_INTERVAL);
6697 }
6698
6699 /* Firmware interface routines. */
6700
6701 #define FW_ISP21XX 0
6702 #define FW_ISP22XX 1
6703 #define FW_ISP2300 2
6704 #define FW_ISP2322 3
6705 #define FW_ISP24XX 4
6706 #define FW_ISP25XX 5
6707 #define FW_ISP81XX 6
6708 #define FW_ISP82XX 7
6709 #define FW_ISP2031 8
6710 #define FW_ISP8031 9
6711 #define FW_ISP27XX 10
6712 #define FW_ISP28XX 11
6713
6714 #define FW_FILE_ISP21XX "ql2100_fw.bin"
6715 #define FW_FILE_ISP22XX "ql2200_fw.bin"
6716 #define FW_FILE_ISP2300 "ql2300_fw.bin"
6717 #define FW_FILE_ISP2322 "ql2322_fw.bin"
6718 #define FW_FILE_ISP24XX "ql2400_fw.bin"
6719 #define FW_FILE_ISP25XX "ql2500_fw.bin"
6720 #define FW_FILE_ISP81XX "ql8100_fw.bin"
6721 #define FW_FILE_ISP82XX "ql8200_fw.bin"
6722 #define FW_FILE_ISP2031 "ql2600_fw.bin"
6723 #define FW_FILE_ISP8031 "ql8300_fw.bin"
6724 #define FW_FILE_ISP27XX "ql2700_fw.bin"
6725 #define FW_FILE_ISP28XX "ql2800_fw.bin"
6726
6727
6728 static DEFINE_MUTEX(qla_fw_lock);
6729
6730 static struct fw_blob qla_fw_blobs[] = {
6731 { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
6732 { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
6733 { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
6734 { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
6735 { .name = FW_FILE_ISP24XX, },
6736 { .name = FW_FILE_ISP25XX, },
6737 { .name = FW_FILE_ISP81XX, },
6738 { .name = FW_FILE_ISP82XX, },
6739 { .name = FW_FILE_ISP2031, },
6740 { .name = FW_FILE_ISP8031, },
6741 { .name = FW_FILE_ISP27XX, },
6742 { .name = FW_FILE_ISP28XX, },
6743 { .name = NULL, },
6744 };
6745
6746 struct fw_blob *
6747 qla2x00_request_firmware(scsi_qla_host_t *vha)
6748 {
6749 struct qla_hw_data *ha = vha->hw;
6750 struct fw_blob *blob;
6751
6752 if (IS_QLA2100(ha)) {
6753 blob = &qla_fw_blobs[FW_ISP21XX];
6754 } else if (IS_QLA2200(ha)) {
6755 blob = &qla_fw_blobs[FW_ISP22XX];
6756 } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
6757 blob = &qla_fw_blobs[FW_ISP2300];
6758 } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
6759 blob = &qla_fw_blobs[FW_ISP2322];
6760 } else if (IS_QLA24XX_TYPE(ha)) {
6761 blob = &qla_fw_blobs[FW_ISP24XX];
6762 } else if (IS_QLA25XX(ha)) {
6763 blob = &qla_fw_blobs[FW_ISP25XX];
6764 } else if (IS_QLA81XX(ha)) {
6765 blob = &qla_fw_blobs[FW_ISP81XX];
6766 } else if (IS_QLA82XX(ha)) {
6767 blob = &qla_fw_blobs[FW_ISP82XX];
6768 } else if (IS_QLA2031(ha)) {
6769 blob = &qla_fw_blobs[FW_ISP2031];
6770 } else if (IS_QLA8031(ha)) {
6771 blob = &qla_fw_blobs[FW_ISP8031];
6772 } else if (IS_QLA27XX(ha)) {
6773 blob = &qla_fw_blobs[FW_ISP27XX];
6774 } else if (IS_QLA28XX(ha)) {
6775 blob = &qla_fw_blobs[FW_ISP28XX];
6776 } else {
6777 return NULL;
6778 }
6779
6780 if (!blob->name)
6781 return NULL;
6782
6783 mutex_lock(&qla_fw_lock);
6784 if (blob->fw)
6785 goto out;
6786
6787 if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
6788 ql_log(ql_log_warn, vha, 0x0063,
6789 "Failed to load firmware image (%s).\n", blob->name);
6790 blob->fw = NULL;
6791 blob = NULL;
6792 }
6793
6794 out:
6795 mutex_unlock(&qla_fw_lock);
6796 return blob;
6797 }
6798
6799 static void
6800 qla2x00_release_firmware(void)
6801 {
6802 struct fw_blob *blob;
6803
6804 mutex_lock(&qla_fw_lock);
6805 for (blob = qla_fw_blobs; blob->name; blob++)
6806 release_firmware(blob->fw);
6807 mutex_unlock(&qla_fw_lock);
6808 }
6809
6810 static void qla_pci_error_cleanup(scsi_qla_host_t *vha)
6811 {
6812 struct qla_hw_data *ha = vha->hw;
6813 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6814 struct qla_qpair *qpair = NULL;
6815 struct scsi_qla_host *vp;
6816 fc_port_t *fcport;
6817 int i;
6818 unsigned long flags;
6819
6820 ha->chip_reset++;
6821
6822 ha->base_qpair->chip_reset = ha->chip_reset;
6823 for (i = 0; i < ha->max_qpairs; i++) {
6824 if (ha->queue_pair_map[i])
6825 ha->queue_pair_map[i]->chip_reset =
6826 ha->base_qpair->chip_reset;
6827 }
6828
6829 /* purge MBox commands */
6830 if (atomic_read(&ha->num_pend_mbx_stage3)) {
6831 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
6832 complete(&ha->mbx_intr_comp);
6833 }
6834
6835 i = 0;
6836
6837 while (atomic_read(&ha->num_pend_mbx_stage3) ||
6838 atomic_read(&ha->num_pend_mbx_stage2) ||
6839 atomic_read(&ha->num_pend_mbx_stage1)) {
6840 msleep(20);
6841 i++;
6842 if (i > 50)
6843 break;
6844 }
6845
6846 ha->flags.purge_mbox = 0;
6847
6848 mutex_lock(&ha->mq_lock);
6849 list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
6850 qpair->online = 0;
6851 mutex_unlock(&ha->mq_lock);
6852
6853 qla2x00_mark_all_devices_lost(vha, 0);
6854
6855 spin_lock_irqsave(&ha->vport_slock, flags);
6856 list_for_each_entry(vp, &ha->vp_list, list) {
6857 atomic_inc(&vp->vref_count);
6858 spin_unlock_irqrestore(&ha->vport_slock, flags);
6859 qla2x00_mark_all_devices_lost(vp, 0);
6860 spin_lock_irqsave(&ha->vport_slock, flags);
6861 atomic_dec(&vp->vref_count);
6862 }
6863 spin_unlock_irqrestore(&ha->vport_slock, flags);
6864
6865 /* Clear all async request states across all VPs. */
6866 list_for_each_entry(fcport, &vha->vp_fcports, list)
6867 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6868
6869 spin_lock_irqsave(&ha->vport_slock, flags);
6870 list_for_each_entry(vp, &ha->vp_list, list) {
6871 atomic_inc(&vp->vref_count);
6872 spin_unlock_irqrestore(&ha->vport_slock, flags);
6873 list_for_each_entry(fcport, &vp->vp_fcports, list)
6874 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6875 spin_lock_irqsave(&ha->vport_slock, flags);
6876 atomic_dec(&vp->vref_count);
6877 }
6878 spin_unlock_irqrestore(&ha->vport_slock, flags);
6879 }
6880
6881
6882 static pci_ers_result_t
6883 qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
6884 {
6885 scsi_qla_host_t *vha = pci_get_drvdata(pdev);
6886 struct qla_hw_data *ha = vha->hw;
6887
6888 ql_dbg(ql_dbg_aer, vha, 0x9000,
6889 "PCI error detected, state %x.\n", state);
6890
6891 if (!atomic_read(&pdev->enable_cnt)) {
6892 ql_log(ql_log_info, vha, 0xffff,
6893 "PCI device is disabled,state %x\n", state);
6894 return PCI_ERS_RESULT_NEED_RESET;
6895 }
6896
6897 switch (state) {
6898 case pci_channel_io_normal:
6899 ha->flags.eeh_busy = 0;
6900 if (ql2xmqsupport || ql2xnvmeenable) {
6901 set_bit(QPAIR_ONLINE_CHECK_NEEDED, &vha->dpc_flags);
6902 qla2xxx_wake_dpc(vha);
6903 }
6904 return PCI_ERS_RESULT_CAN_RECOVER;
6905 case pci_channel_io_frozen:
6906 ha->flags.eeh_busy = 1;
6907 qla_pci_error_cleanup(vha);
6908 return PCI_ERS_RESULT_NEED_RESET;
6909 case pci_channel_io_perm_failure:
6910 ha->flags.pci_channel_io_perm_failure = 1;
6911 qla2x00_abort_all_cmds(vha, DID_NO_CONNECT << 16);
6912 if (ql2xmqsupport || ql2xnvmeenable) {
6913 set_bit(QPAIR_ONLINE_CHECK_NEEDED, &vha->dpc_flags);
6914 qla2xxx_wake_dpc(vha);
6915 }
6916 return PCI_ERS_RESULT_DISCONNECT;
6917 }
6918 return PCI_ERS_RESULT_NEED_RESET;
6919 }
6920
6921 static pci_ers_result_t
6922 qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
6923 {
6924 int risc_paused = 0;
6925 uint32_t stat;
6926 unsigned long flags;
6927 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
6928 struct qla_hw_data *ha = base_vha->hw;
6929 struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
6930 struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
6931
6932 if (IS_QLA82XX(ha))
6933 return PCI_ERS_RESULT_RECOVERED;
6934
6935 spin_lock_irqsave(&ha->hardware_lock, flags);
6936 if (IS_QLA2100(ha) || IS_QLA2200(ha)){
6937 stat = RD_REG_DWORD(&reg->hccr);
6938 if (stat & HCCR_RISC_PAUSE)
6939 risc_paused = 1;
6940 } else if (IS_QLA23XX(ha)) {
6941 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
6942 if (stat & HSR_RISC_PAUSED)
6943 risc_paused = 1;
6944 } else if (IS_FWI2_CAPABLE(ha)) {
6945 stat = RD_REG_DWORD(&reg24->host_status);
6946 if (stat & HSRX_RISC_PAUSED)
6947 risc_paused = 1;
6948 }
6949 spin_unlock_irqrestore(&ha->hardware_lock, flags);
6950
6951 if (risc_paused) {
6952 ql_log(ql_log_info, base_vha, 0x9003,
6953 "RISC paused -- mmio_enabled, Dumping firmware.\n");
6954 ha->isp_ops->fw_dump(base_vha, 0);
6955
6956 return PCI_ERS_RESULT_NEED_RESET;
6957 } else
6958 return PCI_ERS_RESULT_RECOVERED;
6959 }
6960
6961 static pci_ers_result_t
6962 qla2xxx_pci_slot_reset(struct pci_dev *pdev)
6963 {
6964 pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
6965 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
6966 struct qla_hw_data *ha = base_vha->hw;
6967 int rc;
6968 struct qla_qpair *qpair = NULL;
6969
6970 ql_dbg(ql_dbg_aer, base_vha, 0x9004,
6971 "Slot Reset.\n");
6972
6973 /* Workaround: qla2xxx driver which access hardware earlier
6974 * needs error state to be pci_channel_io_online.
6975 * Otherwise mailbox command timesout.
6976 */
6977 pdev->error_state = pci_channel_io_normal;
6978
6979 pci_restore_state(pdev);
6980
6981 /* pci_restore_state() clears the saved_state flag of the device
6982 * save restored state which resets saved_state flag
6983 */
6984 pci_save_state(pdev);
6985
6986 if (ha->mem_only)
6987 rc = pci_enable_device_mem(pdev);
6988 else
6989 rc = pci_enable_device(pdev);
6990
6991 if (rc) {
6992 ql_log(ql_log_warn, base_vha, 0x9005,
6993 "Can't re-enable PCI device after reset.\n");
6994 goto exit_slot_reset;
6995 }
6996
6997
6998 if (ha->isp_ops->pci_config(base_vha))
6999 goto exit_slot_reset;
7000
7001 mutex_lock(&ha->mq_lock);
7002 list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7003 qpair->online = 1;
7004 mutex_unlock(&ha->mq_lock);
7005
7006 base_vha->flags.online = 1;
7007 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7008 if (ha->isp_ops->abort_isp(base_vha) == QLA_SUCCESS)
7009 ret = PCI_ERS_RESULT_RECOVERED;
7010 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7011
7012
7013 exit_slot_reset:
7014 ql_dbg(ql_dbg_aer, base_vha, 0x900e,
7015 "slot_reset return %x.\n", ret);
7016
7017 return ret;
7018 }
7019
7020 static void
7021 qla2xxx_pci_resume(struct pci_dev *pdev)
7022 {
7023 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7024 struct qla_hw_data *ha = base_vha->hw;
7025 int ret;
7026
7027 ql_dbg(ql_dbg_aer, base_vha, 0x900f,
7028 "pci_resume.\n");
7029
7030 ha->flags.eeh_busy = 0;
7031
7032 ret = qla2x00_wait_for_hba_online(base_vha);
7033 if (ret != QLA_SUCCESS) {
7034 ql_log(ql_log_fatal, base_vha, 0x9002,
7035 "The device failed to resume I/O from slot/link_reset.\n");
7036 }
7037 }
7038
7039 static void
7040 qla_pci_reset_prepare(struct pci_dev *pdev)
7041 {
7042 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7043 struct qla_hw_data *ha = base_vha->hw;
7044 struct qla_qpair *qpair;
7045
7046 ql_log(ql_log_warn, base_vha, 0xffff,
7047 "%s.\n", __func__);
7048
7049 /*
7050 * PCI FLR/function reset is about to reset the
7051 * slot. Stop the chip to stop all DMA access.
7052 * It is assumed that pci_reset_done will be called
7053 * after FLR to resume Chip operation.
7054 */
7055 ha->flags.eeh_busy = 1;
7056 mutex_lock(&ha->mq_lock);
7057 list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7058 qpair->online = 0;
7059 mutex_unlock(&ha->mq_lock);
7060
7061 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7062 qla2x00_abort_isp_cleanup(base_vha);
7063 qla2x00_abort_all_cmds(base_vha, DID_RESET << 16);
7064 }
7065
7066 static void
7067 qla_pci_reset_done(struct pci_dev *pdev)
7068 {
7069 scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
7070 struct qla_hw_data *ha = base_vha->hw;
7071 struct qla_qpair *qpair;
7072
7073 ql_log(ql_log_warn, base_vha, 0xffff,
7074 "%s.\n", __func__);
7075
7076 /*
7077 * FLR just completed by PCI layer. Resume adapter
7078 */
7079 ha->flags.eeh_busy = 0;
7080 mutex_lock(&ha->mq_lock);
7081 list_for_each_entry(qpair, &base_vha->qp_list, qp_list_elem)
7082 qpair->online = 1;
7083 mutex_unlock(&ha->mq_lock);
7084
7085 base_vha->flags.online = 1;
7086 ha->isp_ops->abort_isp(base_vha);
7087 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
7088 }
7089
7090 static int qla2xxx_map_queues(struct Scsi_Host *shost)
7091 {
7092 int rc;
7093 scsi_qla_host_t *vha = (scsi_qla_host_t *)shost->hostdata;
7094 struct blk_mq_queue_map *qmap = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
7095
7096 if (USER_CTRL_IRQ(vha->hw) || !vha->hw->mqiobase)
7097 rc = blk_mq_map_queues(qmap);
7098 else
7099 rc = blk_mq_pci_map_queues(qmap, vha->hw->pdev, vha->irq_offset);
7100 return rc;
7101 }
7102
7103 struct scsi_host_template qla2xxx_driver_template = {
7104 .module = THIS_MODULE,
7105 .name = QLA2XXX_DRIVER_NAME,
7106 .queuecommand = qla2xxx_queuecommand,
7107
7108 .eh_timed_out = fc_eh_timed_out,
7109 .eh_abort_handler = qla2xxx_eh_abort,
7110 .eh_device_reset_handler = qla2xxx_eh_device_reset,
7111 .eh_target_reset_handler = qla2xxx_eh_target_reset,
7112 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
7113 .eh_host_reset_handler = qla2xxx_eh_host_reset,
7114
7115 .slave_configure = qla2xxx_slave_configure,
7116
7117 .slave_alloc = qla2xxx_slave_alloc,
7118 .slave_destroy = qla2xxx_slave_destroy,
7119 .scan_finished = qla2xxx_scan_finished,
7120 .scan_start = qla2xxx_scan_start,
7121 .change_queue_depth = scsi_change_queue_depth,
7122 .map_queues = qla2xxx_map_queues,
7123 .this_id = -1,
7124 .cmd_per_lun = 3,
7125 .sg_tablesize = SG_ALL,
7126
7127 .max_sectors = 0xFFFF,
7128 .shost_attrs = qla2x00_host_attrs,
7129
7130 .supported_mode = MODE_INITIATOR,
7131 .track_queue_depth = 1,
7132 .cmd_size = sizeof(srb_t),
7133 };
7134
7135 static const struct pci_error_handlers qla2xxx_err_handler = {
7136 .error_detected = qla2xxx_pci_error_detected,
7137 .mmio_enabled = qla2xxx_pci_mmio_enabled,
7138 .slot_reset = qla2xxx_pci_slot_reset,
7139 .resume = qla2xxx_pci_resume,
7140 .reset_prepare = qla_pci_reset_prepare,
7141 .reset_done = qla_pci_reset_done,
7142 };
7143
7144 static struct pci_device_id qla2xxx_pci_tbl[] = {
7145 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
7146 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
7147 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
7148 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
7149 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
7150 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
7151 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
7152 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
7153 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
7154 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
7155 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
7156 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
7157 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
7158 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2031) },
7159 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
7160 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8021) },
7161 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8031) },
7162 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISPF001) },
7163 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8044) },
7164 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2071) },
7165 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2271) },
7166 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2261) },
7167 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2061) },
7168 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2081) },
7169 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2281) },
7170 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2089) },
7171 { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2289) },
7172 { 0 },
7173 };
7174 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
7175
7176 static struct pci_driver qla2xxx_pci_driver = {
7177 .name = QLA2XXX_DRIVER_NAME,
7178 .driver = {
7179 .owner = THIS_MODULE,
7180 },
7181 .id_table = qla2xxx_pci_tbl,
7182 .probe = qla2x00_probe_one,
7183 .remove = qla2x00_remove_one,
7184 .shutdown = qla2x00_shutdown,
7185 .err_handler = &qla2xxx_err_handler,
7186 };
7187
7188 static const struct file_operations apidev_fops = {
7189 .owner = THIS_MODULE,
7190 .llseek = noop_llseek,
7191 };
7192
7193 /**
7194 * qla2x00_module_init - Module initialization.
7195 **/
7196 static int __init
7197 qla2x00_module_init(void)
7198 {
7199 int ret = 0;
7200
7201 BUILD_BUG_ON(sizeof(cmd_entry_t) != 64);
7202 BUILD_BUG_ON(sizeof(cont_a64_entry_t) != 64);
7203 BUILD_BUG_ON(sizeof(cont_entry_t) != 64);
7204 BUILD_BUG_ON(sizeof(init_cb_t) != 96);
7205 BUILD_BUG_ON(sizeof(ms_iocb_entry_t) != 64);
7206 BUILD_BUG_ON(sizeof(request_t) != 64);
7207 BUILD_BUG_ON(sizeof(struct access_chip_84xx) != 64);
7208 BUILD_BUG_ON(sizeof(struct cmd_bidir) != 64);
7209 BUILD_BUG_ON(sizeof(struct cmd_nvme) != 64);
7210 BUILD_BUG_ON(sizeof(struct cmd_type_6) != 64);
7211 BUILD_BUG_ON(sizeof(struct cmd_type_7) != 64);
7212 BUILD_BUG_ON(sizeof(struct cmd_type_7_fx00) != 64);
7213 BUILD_BUG_ON(sizeof(struct cmd_type_crc_2) != 64);
7214 BUILD_BUG_ON(sizeof(struct ct_entry_24xx) != 64);
7215 BUILD_BUG_ON(sizeof(struct ctio_crc2_to_fw) != 64);
7216 BUILD_BUG_ON(sizeof(struct els_entry_24xx) != 64);
7217 BUILD_BUG_ON(sizeof(struct fxdisc_entry_fx00) != 64);
7218 BUILD_BUG_ON(sizeof(struct init_cb_24xx) != 128);
7219 BUILD_BUG_ON(sizeof(struct init_cb_81xx) != 128);
7220 BUILD_BUG_ON(sizeof(struct pt_ls4_request) != 64);
7221 BUILD_BUG_ON(sizeof(struct sns_cmd_pkt) != 2064);
7222 BUILD_BUG_ON(sizeof(struct verify_chip_entry_84xx) != 64);
7223 BUILD_BUG_ON(sizeof(struct vf_evfp_entry_24xx) != 56);
7224
7225 /* Allocate cache for SRBs. */
7226 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
7227 SLAB_HWCACHE_ALIGN, NULL);
7228 if (srb_cachep == NULL) {
7229 ql_log(ql_log_fatal, NULL, 0x0001,
7230 "Unable to allocate SRB cache...Failing load!.\n");
7231 return -ENOMEM;
7232 }
7233
7234 /* Initialize target kmem_cache and mem_pools */
7235 ret = qlt_init();
7236 if (ret < 0) {
7237 goto destroy_cache;
7238 } else if (ret > 0) {
7239 /*
7240 * If initiator mode is explictly disabled by qlt_init(),
7241 * prevent scsi_transport_fc.c:fc_scsi_scan_rport() from
7242 * performing scsi_scan_target() during LOOP UP event.
7243 */
7244 qla2xxx_transport_functions.disable_target_scan = 1;
7245 qla2xxx_transport_vport_functions.disable_target_scan = 1;
7246 }
7247
7248 /* Derive version string. */
7249 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
7250 if (ql2xextended_error_logging)
7251 strcat(qla2x00_version_str, "-debug");
7252 if (ql2xextended_error_logging == 1)
7253 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
7254
7255 if (ql2x_ini_mode == QLA2XXX_INI_MODE_DUAL)
7256 qla_insert_tgt_attrs();
7257
7258 qla2xxx_transport_template =
7259 fc_attach_transport(&qla2xxx_transport_functions);
7260 if (!qla2xxx_transport_template) {
7261 ql_log(ql_log_fatal, NULL, 0x0002,
7262 "fc_attach_transport failed...Failing load!.\n");
7263 ret = -ENODEV;
7264 goto qlt_exit;
7265 }
7266
7267 apidev_major = register_chrdev(0, QLA2XXX_APIDEV, &apidev_fops);
7268 if (apidev_major < 0) {
7269 ql_log(ql_log_fatal, NULL, 0x0003,
7270 "Unable to register char device %s.\n", QLA2XXX_APIDEV);
7271 }
7272
7273 qla2xxx_transport_vport_template =
7274 fc_attach_transport(&qla2xxx_transport_vport_functions);
7275 if (!qla2xxx_transport_vport_template) {
7276 ql_log(ql_log_fatal, NULL, 0x0004,
7277 "fc_attach_transport vport failed...Failing load!.\n");
7278 ret = -ENODEV;
7279 goto unreg_chrdev;
7280 }
7281 ql_log(ql_log_info, NULL, 0x0005,
7282 "QLogic Fibre Channel HBA Driver: %s.\n",
7283 qla2x00_version_str);
7284 ret = pci_register_driver(&qla2xxx_pci_driver);
7285 if (ret) {
7286 ql_log(ql_log_fatal, NULL, 0x0006,
7287 "pci_register_driver failed...ret=%d Failing load!.\n",
7288 ret);
7289 goto release_vport_transport;
7290 }
7291 return ret;
7292
7293 release_vport_transport:
7294 fc_release_transport(qla2xxx_transport_vport_template);
7295
7296 unreg_chrdev:
7297 if (apidev_major >= 0)
7298 unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
7299 fc_release_transport(qla2xxx_transport_template);
7300
7301 qlt_exit:
7302 qlt_exit();
7303
7304 destroy_cache:
7305 kmem_cache_destroy(srb_cachep);
7306 return ret;
7307 }
7308
7309 /**
7310 * qla2x00_module_exit - Module cleanup.
7311 **/
7312 static void __exit
7313 qla2x00_module_exit(void)
7314 {
7315 pci_unregister_driver(&qla2xxx_pci_driver);
7316 qla2x00_release_firmware();
7317 kmem_cache_destroy(ctx_cachep);
7318 fc_release_transport(qla2xxx_transport_vport_template);
7319 if (apidev_major >= 0)
7320 unregister_chrdev(apidev_major, QLA2XXX_APIDEV);
7321 fc_release_transport(qla2xxx_transport_template);
7322 qlt_exit();
7323 kmem_cache_destroy(srb_cachep);
7324 }
7325
7326 module_init(qla2x00_module_init);
7327 module_exit(qla2x00_module_exit);
7328
7329 MODULE_AUTHOR("QLogic Corporation");
7330 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
7331 MODULE_LICENSE("GPL");
7332 MODULE_VERSION(QLA2XXX_VERSION);
7333 MODULE_FIRMWARE(FW_FILE_ISP21XX);
7334 MODULE_FIRMWARE(FW_FILE_ISP22XX);
7335 MODULE_FIRMWARE(FW_FILE_ISP2300);
7336 MODULE_FIRMWARE(FW_FILE_ISP2322);
7337 MODULE_FIRMWARE(FW_FILE_ISP24XX);
7338 MODULE_FIRMWARE(FW_FILE_ISP25XX);