GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / scsi / qla2xxx / qla_mid.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic Fibre Channel HBA Driver
4  * Copyright (c)  2003-2014 QLogic Corporation
5  */
6 #include "qla_def.h"
7 #include "qla_gbl.h"
8 #include "qla_target.h"
9
10 #include <linux/moduleparam.h>
11 #include <linux/vmalloc.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
14
15 #include <scsi/scsi_tcq.h>
16 #include <scsi/scsicam.h>
17 #include <linux/delay.h>
18
19 void
20 qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
21 {
22         if (vha->vp_idx && vha->timer_active) {
23                 del_timer_sync(&vha->timer);
24                 vha->timer_active = 0;
25         }
26 }
27
28 static uint32_t
29 qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
30 {
31         uint32_t vp_id;
32         struct qla_hw_data *ha = vha->hw;
33         unsigned long flags;
34
35         /* Find an empty slot and assign an vp_id */
36         mutex_lock(&ha->vport_lock);
37         vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
38         if (vp_id > ha->max_npiv_vports) {
39                 ql_dbg(ql_dbg_vport, vha, 0xa000,
40                     "vp_id %d is bigger than max-supported %d.\n",
41                     vp_id, ha->max_npiv_vports);
42                 mutex_unlock(&ha->vport_lock);
43                 return vp_id;
44         }
45
46         set_bit(vp_id, ha->vp_idx_map);
47         ha->num_vhosts++;
48         vha->vp_idx = vp_id;
49
50         spin_lock_irqsave(&ha->vport_slock, flags);
51         list_add_tail(&vha->list, &ha->vp_list);
52         spin_unlock_irqrestore(&ha->vport_slock, flags);
53
54         spin_lock_irqsave(&ha->hardware_lock, flags);
55         qlt_update_vp_map(vha, SET_VP_IDX);
56         spin_unlock_irqrestore(&ha->hardware_lock, flags);
57
58         mutex_unlock(&ha->vport_lock);
59         return vp_id;
60 }
61
62 void
63 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
64 {
65         uint16_t vp_id;
66         struct qla_hw_data *ha = vha->hw;
67         unsigned long flags = 0;
68         u32 i, bailout;
69
70         mutex_lock(&ha->vport_lock);
71         /*
72          * Wait for all pending activities to finish before removing vport from
73          * the list.
74          * Lock needs to be held for safe removal from the list (it
75          * ensures no active vp_list traversal while the vport is removed
76          * from the queue)
77          */
78         bailout = 0;
79         for (i = 0; i < 500; i++) {
80                 spin_lock_irqsave(&ha->vport_slock, flags);
81                 if (atomic_read(&vha->vref_count) == 0) {
82                         list_del(&vha->list);
83                         qlt_update_vp_map(vha, RESET_VP_IDX);
84                         bailout = 1;
85                 }
86                 spin_unlock_irqrestore(&ha->vport_slock, flags);
87
88                 if (bailout)
89                         break;
90                 else
91                         msleep(20);
92         }
93         if (!bailout) {
94                 ql_log(ql_log_info, vha, 0xfffa,
95                         "vha->vref_count=%u timeout\n", vha->vref_count.counter);
96                 spin_lock_irqsave(&ha->vport_slock, flags);
97                 list_del(&vha->list);
98                 qlt_update_vp_map(vha, RESET_VP_IDX);
99                 spin_unlock_irqrestore(&ha->vport_slock, flags);
100         }
101
102         vp_id = vha->vp_idx;
103         ha->num_vhosts--;
104         clear_bit(vp_id, ha->vp_idx_map);
105
106         mutex_unlock(&ha->vport_lock);
107 }
108
109 static scsi_qla_host_t *
110 qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
111 {
112         scsi_qla_host_t *vha;
113         struct scsi_qla_host *tvha;
114         unsigned long flags;
115
116         spin_lock_irqsave(&ha->vport_slock, flags);
117         /* Locate matching device in database. */
118         list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
119                 if (!memcmp(port_name, vha->port_name, WWN_SIZE)) {
120                         spin_unlock_irqrestore(&ha->vport_slock, flags);
121                         return vha;
122                 }
123         }
124         spin_unlock_irqrestore(&ha->vport_slock, flags);
125         return NULL;
126 }
127
128 /*
129  * qla2x00_mark_vp_devices_dead
130  *      Updates fcport state when device goes offline.
131  *
132  * Input:
133  *      ha = adapter block pointer.
134  *      fcport = port structure pointer.
135  *
136  * Return:
137  *      None.
138  *
139  * Context:
140  */
141 static void
142 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
143 {
144         /*
145          * !!! NOTE !!!
146          * This function, if called in contexts other than vp create, disable
147          * or delete, please make sure this is synchronized with the
148          * delete thread.
149          */
150         fc_port_t *fcport;
151
152         list_for_each_entry(fcport, &vha->vp_fcports, list) {
153                 ql_dbg(ql_dbg_vport, vha, 0xa001,
154                     "Marking port dead, loop_id=0x%04x : %x.\n",
155                     fcport->loop_id, fcport->vha->vp_idx);
156
157                 qla2x00_mark_device_lost(vha, fcport, 0);
158                 qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
159         }
160 }
161
162 int
163 qla24xx_disable_vp(scsi_qla_host_t *vha)
164 {
165         unsigned long flags;
166         int ret = QLA_SUCCESS;
167         fc_port_t *fcport;
168
169         if (vha->hw->flags.edif_enabled)
170                 /* delete sessions and flush sa_indexes */
171                 qla2x00_wait_for_sess_deletion(vha);
172
173         if (vha->hw->flags.fw_started)
174                 ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
175
176         atomic_set(&vha->loop_state, LOOP_DOWN);
177         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
178         list_for_each_entry(fcport, &vha->vp_fcports, list)
179                 fcport->logout_on_delete = 0;
180
181         if (!vha->hw->flags.edif_enabled)
182                 qla2x00_wait_for_sess_deletion(vha);
183
184         /* Remove port id from vp target map */
185         spin_lock_irqsave(&vha->hw->hardware_lock, flags);
186         qlt_update_vp_map(vha, RESET_AL_PA);
187         spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
188
189         qla2x00_mark_vp_devices_dead(vha);
190         atomic_set(&vha->vp_state, VP_FAILED);
191         vha->flags.management_server_logged_in = 0;
192         if (ret == QLA_SUCCESS) {
193                 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
194         } else {
195                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
196                 return -1;
197         }
198         return 0;
199 }
200
201 int
202 qla24xx_enable_vp(scsi_qla_host_t *vha)
203 {
204         int ret;
205         struct qla_hw_data *ha = vha->hw;
206         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
207
208         /* Check if physical ha port is Up */
209         if (atomic_read(&base_vha->loop_state) == LOOP_DOWN  ||
210                 atomic_read(&base_vha->loop_state) == LOOP_DEAD ||
211                 !(ha->current_topology & ISP_CFG_F)) {
212                 vha->vp_err_state =  VP_ERR_PORTDWN;
213                 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
214                 ql_dbg(ql_dbg_taskm, vha, 0x800b,
215                     "%s skip enable. loop_state %x topo %x\n",
216                     __func__, base_vha->loop_state.counter,
217                     ha->current_topology);
218
219                 goto enable_failed;
220         }
221
222         /* Initialize the new vport unless it is a persistent port */
223         mutex_lock(&ha->vport_lock);
224         ret = qla24xx_modify_vp_config(vha);
225         mutex_unlock(&ha->vport_lock);
226
227         if (ret != QLA_SUCCESS) {
228                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
229                 goto enable_failed;
230         }
231
232         ql_dbg(ql_dbg_taskm, vha, 0x801a,
233             "Virtual port with id: %d - Enabled.\n", vha->vp_idx);
234         return 0;
235
236 enable_failed:
237         ql_dbg(ql_dbg_taskm, vha, 0x801b,
238             "Virtual port with id: %d - Disabled.\n", vha->vp_idx);
239         return 1;
240 }
241
242 static void
243 qla24xx_configure_vp(scsi_qla_host_t *vha)
244 {
245         struct fc_vport *fc_vport;
246         int ret;
247
248         fc_vport = vha->fc_vport;
249
250         ql_dbg(ql_dbg_vport, vha, 0xa002,
251             "%s: change request #3.\n", __func__);
252         ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
253         if (ret != QLA_SUCCESS) {
254                 ql_dbg(ql_dbg_vport, vha, 0xa003, "Failed to enable "
255                     "receiving of RSCN requests: 0x%x.\n", ret);
256                 return;
257         } else {
258                 /* Corresponds to SCR enabled */
259                 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
260         }
261
262         vha->flags.online = 1;
263         if (qla24xx_configure_vhba(vha))
264                 return;
265
266         atomic_set(&vha->vp_state, VP_ACTIVE);
267         fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
268 }
269
270 void
271 qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
272 {
273         scsi_qla_host_t *vha, *tvp;
274         struct qla_hw_data *ha = rsp->hw;
275         int i = 0;
276         unsigned long flags;
277
278         spin_lock_irqsave(&ha->vport_slock, flags);
279         list_for_each_entry_safe(vha, tvp, &ha->vp_list, list) {
280                 if (vha->vp_idx) {
281                         if (test_bit(VPORT_DELETE, &vha->dpc_flags))
282                                 continue;
283
284                         atomic_inc(&vha->vref_count);
285                         spin_unlock_irqrestore(&ha->vport_slock, flags);
286
287                         switch (mb[0]) {
288                         case MBA_LIP_OCCURRED:
289                         case MBA_LOOP_UP:
290                         case MBA_LOOP_DOWN:
291                         case MBA_LIP_RESET:
292                         case MBA_POINT_TO_POINT:
293                         case MBA_CHG_IN_CONNECTION:
294                                 ql_dbg(ql_dbg_async, vha, 0x5024,
295                                     "Async_event for VP[%d], mb=0x%x vha=%p.\n",
296                                     i, *mb, vha);
297                                 qla2x00_async_event(vha, rsp, mb);
298                                 break;
299                         case MBA_PORT_UPDATE:
300                         case MBA_RSCN_UPDATE:
301                                 if ((mb[3] & 0xff) == vha->vp_idx) {
302                                         ql_dbg(ql_dbg_async, vha, 0x5024,
303                                             "Async_event for VP[%d], mb=0x%x vha=%p\n",
304                                             i, *mb, vha);
305                                         qla2x00_async_event(vha, rsp, mb);
306                                 }
307                                 break;
308                         }
309
310                         spin_lock_irqsave(&ha->vport_slock, flags);
311                         atomic_dec(&vha->vref_count);
312                         wake_up(&vha->vref_waitq);
313                 }
314                 i++;
315         }
316         spin_unlock_irqrestore(&ha->vport_slock, flags);
317 }
318
319 int
320 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
321 {
322         fc_port_t *fcport;
323
324         /*
325          * To exclusively reset vport, we need to log it out first.
326          * Note: This control_vp can fail if ISP reset is already
327          * issued, this is expected, as the vp would be already
328          * logged out due to ISP reset.
329          */
330         if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags)) {
331                 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
332                 list_for_each_entry(fcport, &vha->vp_fcports, list)
333                         fcport->logout_on_delete = 0;
334         }
335
336         /*
337          * Physical port will do most of the abort and recovery work. We can
338          * just treat it as a loop down
339          */
340         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
341                 atomic_set(&vha->loop_state, LOOP_DOWN);
342                 qla2x00_mark_all_devices_lost(vha);
343         } else {
344                 if (!atomic_read(&vha->loop_down_timer))
345                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
346         }
347
348         ql_dbg(ql_dbg_taskm, vha, 0x801d,
349             "Scheduling enable of Vport %d.\n", vha->vp_idx);
350
351         return qla24xx_enable_vp(vha);
352 }
353
354 static int
355 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
356 {
357         struct qla_hw_data *ha = vha->hw;
358         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
359
360         ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x4012,
361             "Entering %s vp_flags: 0x%lx.\n", __func__, vha->vp_flags);
362
363         /* Check if Fw is ready to configure VP first */
364         if (test_bit(VP_CONFIG_OK, &base_vha->vp_flags)) {
365                 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
366                         /* VP acquired. complete port configuration */
367                         ql_dbg(ql_dbg_dpc, vha, 0x4014,
368                             "Configure VP scheduled.\n");
369                         qla24xx_configure_vp(vha);
370                         ql_dbg(ql_dbg_dpc, vha, 0x4015,
371                             "Configure VP end.\n");
372                         return 0;
373                 }
374         }
375
376         if (test_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags)) {
377                 if (atomic_read(&vha->loop_state) == LOOP_READY) {
378                         qla24xx_process_purex_list(&vha->purex_list);
379                         clear_bit(PROCESS_PUREX_IOCB, &vha->dpc_flags);
380                 }
381         }
382
383         if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
384                 ql_dbg(ql_dbg_dpc, vha, 0x4016,
385                     "FCPort update scheduled.\n");
386                 qla2x00_update_fcports(vha);
387                 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
388                 ql_dbg(ql_dbg_dpc, vha, 0x4017,
389                     "FCPort update end.\n");
390         }
391
392         if (test_bit(RELOGIN_NEEDED, &vha->dpc_flags) &&
393             !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
394             atomic_read(&vha->loop_state) != LOOP_DOWN) {
395
396                 if (!vha->relogin_jif ||
397                     time_after_eq(jiffies, vha->relogin_jif)) {
398                         vha->relogin_jif = jiffies + HZ;
399                         clear_bit(RELOGIN_NEEDED, &vha->dpc_flags);
400
401                         ql_dbg(ql_dbg_dpc, vha, 0x4018,
402                             "Relogin needed scheduled.\n");
403                         qla24xx_post_relogin_work(vha);
404                 }
405         }
406
407         if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
408             (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
409                 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
410         }
411
412         if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
413                 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
414                         ql_dbg(ql_dbg_dpc, vha, 0x401a,
415                             "Loop resync scheduled.\n");
416                         qla2x00_loop_resync(vha);
417                         clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
418                         ql_dbg(ql_dbg_dpc, vha, 0x401b,
419                             "Loop resync end.\n");
420                 }
421         }
422
423         ql_dbg(ql_dbg_dpc + ql_dbg_verbose, vha, 0x401c,
424             "Exiting %s.\n", __func__);
425         return 0;
426 }
427
428 void
429 qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
430 {
431         struct qla_hw_data *ha = vha->hw;
432         scsi_qla_host_t *vp, *tvp;
433         unsigned long flags = 0;
434
435         if (vha->vp_idx)
436                 return;
437         if (list_empty(&ha->vp_list))
438                 return;
439
440         clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
441
442         if (!(ha->current_topology & ISP_CFG_F))
443                 return;
444
445         spin_lock_irqsave(&ha->vport_slock, flags);
446         list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
447                 if (vp->vp_idx) {
448                         atomic_inc(&vp->vref_count);
449                         spin_unlock_irqrestore(&ha->vport_slock, flags);
450
451                         qla2x00_do_dpc_vp(vp);
452
453                         spin_lock_irqsave(&ha->vport_slock, flags);
454                         atomic_dec(&vp->vref_count);
455                 }
456         }
457         spin_unlock_irqrestore(&ha->vport_slock, flags);
458 }
459
460 int
461 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
462 {
463         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
464         struct qla_hw_data *ha = base_vha->hw;
465         scsi_qla_host_t *vha;
466         uint8_t port_name[WWN_SIZE];
467
468         if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
469                 return VPCERR_UNSUPPORTED;
470
471         /* Check up the F/W and H/W support NPIV */
472         if (!ha->flags.npiv_supported)
473                 return VPCERR_UNSUPPORTED;
474
475         /* Check up whether npiv supported switch presented */
476         if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
477                 return VPCERR_NO_FABRIC_SUPP;
478
479         /* Check up unique WWPN */
480         u64_to_wwn(fc_vport->port_name, port_name);
481         if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
482                 return VPCERR_BAD_WWN;
483         vha = qla24xx_find_vhost_by_name(ha, port_name);
484         if (vha)
485                 return VPCERR_BAD_WWN;
486
487         /* Check up max-npiv-supports */
488         if (ha->num_vhosts > ha->max_npiv_vports) {
489                 ql_dbg(ql_dbg_vport, vha, 0xa004,
490                     "num_vhosts %ud is bigger "
491                     "than max_npiv_vports %ud.\n",
492                     ha->num_vhosts, ha->max_npiv_vports);
493                 return VPCERR_UNSUPPORTED;
494         }
495         return 0;
496 }
497
498 scsi_qla_host_t *
499 qla24xx_create_vhost(struct fc_vport *fc_vport)
500 {
501         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
502         struct qla_hw_data *ha = base_vha->hw;
503         scsi_qla_host_t *vha;
504         struct scsi_host_template *sht = &qla2xxx_driver_template;
505         struct Scsi_Host *host;
506
507         vha = qla2x00_create_host(sht, ha);
508         if (!vha) {
509                 ql_log(ql_log_warn, vha, 0xa005,
510                     "scsi_host_alloc() failed for vport.\n");
511                 return(NULL);
512         }
513
514         host = vha->host;
515         fc_vport->dd_data = vha;
516         /* New host info */
517         u64_to_wwn(fc_vport->node_name, vha->node_name);
518         u64_to_wwn(fc_vport->port_name, vha->port_name);
519
520         vha->fc_vport = fc_vport;
521         vha->device_flags = 0;
522         vha->vp_idx = qla24xx_allocate_vp_id(vha);
523         if (vha->vp_idx > ha->max_npiv_vports) {
524                 ql_dbg(ql_dbg_vport, vha, 0xa006,
525                     "Couldn't allocate vp_id.\n");
526                 goto create_vhost_failed;
527         }
528         vha->mgmt_svr_loop_id = qla2x00_reserve_mgmt_server_loop_id(vha);
529
530         vha->dpc_flags = 0L;
531         ha->dpc_active = 0;
532         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
533         set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
534
535         /*
536          * To fix the issue of processing a parent's RSCN for the vport before
537          * its SCR is complete.
538          */
539         set_bit(VP_SCR_NEEDED, &vha->vp_flags);
540         atomic_set(&vha->loop_state, LOOP_DOWN);
541         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
542
543         qla2x00_start_timer(vha, WATCH_INTERVAL);
544
545         vha->req = base_vha->req;
546         vha->flags.nvme_enabled = base_vha->flags.nvme_enabled;
547         host->can_queue = base_vha->req->length + 128;
548         host->cmd_per_lun = 3;
549         if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif)
550                 host->max_cmd_len = 32;
551         else
552                 host->max_cmd_len = MAX_CMDSZ;
553         host->max_channel = MAX_BUSES - 1;
554         host->max_lun = ql2xmaxlun;
555         host->unique_id = host->host_no;
556         host->max_id = ha->max_fibre_devices;
557         host->transportt = qla2xxx_transport_vport_template;
558
559         ql_dbg(ql_dbg_vport, vha, 0xa007,
560             "Detect vport hba %ld at address = %p.\n",
561             vha->host_no, vha);
562
563         vha->flags.init_done = 1;
564
565         mutex_lock(&ha->vport_lock);
566         set_bit(vha->vp_idx, ha->vp_idx_map);
567         ha->cur_vport_count++;
568         mutex_unlock(&ha->vport_lock);
569
570         return vha;
571
572 create_vhost_failed:
573         return NULL;
574 }
575
576 static void
577 qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
578 {
579         struct qla_hw_data *ha = vha->hw;
580         uint16_t que_id = req->id;
581
582         dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
583                 sizeof(request_t), req->ring, req->dma);
584         req->ring = NULL;
585         req->dma = 0;
586         if (que_id) {
587                 ha->req_q_map[que_id] = NULL;
588                 mutex_lock(&ha->vport_lock);
589                 clear_bit(que_id, ha->req_qid_map);
590                 mutex_unlock(&ha->vport_lock);
591         }
592         kfree(req->outstanding_cmds);
593         kfree(req);
594 }
595
596 static void
597 qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
598 {
599         struct qla_hw_data *ha = vha->hw;
600         uint16_t que_id = rsp->id;
601
602         if (rsp->msix && rsp->msix->have_irq) {
603                 free_irq(rsp->msix->vector, rsp->msix->handle);
604                 rsp->msix->have_irq = 0;
605                 rsp->msix->in_use = 0;
606                 rsp->msix->handle = NULL;
607         }
608         dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
609                 sizeof(response_t), rsp->ring, rsp->dma);
610         rsp->ring = NULL;
611         rsp->dma = 0;
612         if (que_id) {
613                 ha->rsp_q_map[que_id] = NULL;
614                 mutex_lock(&ha->vport_lock);
615                 clear_bit(que_id, ha->rsp_qid_map);
616                 mutex_unlock(&ha->vport_lock);
617         }
618         kfree(rsp);
619 }
620
621 int
622 qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
623 {
624         int ret = QLA_SUCCESS;
625
626         if (req && vha->flags.qpairs_req_created) {
627                 req->options |= BIT_0;
628                 ret = qla25xx_init_req_que(vha, req);
629                 if (ret != QLA_SUCCESS)
630                         return QLA_FUNCTION_FAILED;
631
632                 qla25xx_free_req_que(vha, req);
633         }
634
635         return ret;
636 }
637
638 int
639 qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
640 {
641         int ret = QLA_SUCCESS;
642
643         if (rsp && vha->flags.qpairs_rsp_created) {
644                 rsp->options |= BIT_0;
645                 ret = qla25xx_init_rsp_que(vha, rsp);
646                 if (ret != QLA_SUCCESS)
647                         return QLA_FUNCTION_FAILED;
648
649                 qla25xx_free_rsp_que(vha, rsp);
650         }
651
652         return ret;
653 }
654
655 /* Delete all queues for a given vhost */
656 int
657 qla25xx_delete_queues(struct scsi_qla_host *vha)
658 {
659         int cnt, ret = 0;
660         struct req_que *req = NULL;
661         struct rsp_que *rsp = NULL;
662         struct qla_hw_data *ha = vha->hw;
663         struct qla_qpair *qpair, *tqpair;
664
665         if (ql2xmqsupport || ql2xnvmeenable) {
666                 list_for_each_entry_safe(qpair, tqpair, &vha->qp_list,
667                     qp_list_elem)
668                         qla2xxx_delete_qpair(vha, qpair);
669         } else {
670                 /* Delete request queues */
671                 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
672                         req = ha->req_q_map[cnt];
673                         if (req && test_bit(cnt, ha->req_qid_map)) {
674                                 ret = qla25xx_delete_req_que(vha, req);
675                                 if (ret != QLA_SUCCESS) {
676                                         ql_log(ql_log_warn, vha, 0x00ea,
677                                             "Couldn't delete req que %d.\n",
678                                             req->id);
679                                         return ret;
680                                 }
681                         }
682                 }
683
684                 /* Delete response queues */
685                 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
686                         rsp = ha->rsp_q_map[cnt];
687                         if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
688                                 ret = qla25xx_delete_rsp_que(vha, rsp);
689                                 if (ret != QLA_SUCCESS) {
690                                         ql_log(ql_log_warn, vha, 0x00eb,
691                                             "Couldn't delete rsp que %d.\n",
692                                             rsp->id);
693                                         return ret;
694                                 }
695                         }
696                 }
697         }
698
699         return ret;
700 }
701
702 int
703 qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
704     uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos, bool startqp)
705 {
706         int ret = 0;
707         struct req_que *req = NULL;
708         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
709         struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
710         uint16_t que_id = 0;
711         device_reg_t *reg;
712         uint32_t cnt;
713
714         req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
715         if (req == NULL) {
716                 ql_log(ql_log_fatal, base_vha, 0x00d9,
717                     "Failed to allocate memory for request queue.\n");
718                 goto failed;
719         }
720
721         req->length = REQUEST_ENTRY_CNT_24XX;
722         req->ring = dma_alloc_coherent(&ha->pdev->dev,
723                         (req->length + 1) * sizeof(request_t),
724                         &req->dma, GFP_KERNEL);
725         if (req->ring == NULL) {
726                 ql_log(ql_log_fatal, base_vha, 0x00da,
727                     "Failed to allocate memory for request_ring.\n");
728                 goto que_failed;
729         }
730
731         ret = qla2x00_alloc_outstanding_cmds(ha, req);
732         if (ret != QLA_SUCCESS)
733                 goto que_failed;
734
735         mutex_lock(&ha->mq_lock);
736         que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
737         if (que_id >= ha->max_req_queues) {
738                 mutex_unlock(&ha->mq_lock);
739                 ql_log(ql_log_warn, base_vha, 0x00db,
740                     "No resources to create additional request queue.\n");
741                 goto que_failed;
742         }
743         set_bit(que_id, ha->req_qid_map);
744         ha->req_q_map[que_id] = req;
745         req->rid = rid;
746         req->vp_idx = vp_idx;
747         req->qos = qos;
748
749         ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
750             "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
751             que_id, req->rid, req->vp_idx, req->qos);
752         ql_dbg(ql_dbg_init, base_vha, 0x00dc,
753             "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
754             que_id, req->rid, req->vp_idx, req->qos);
755         if (rsp_que < 0)
756                 req->rsp = NULL;
757         else
758                 req->rsp = ha->rsp_q_map[rsp_que];
759         /* Use alternate PCI bus number */
760         if (MSB(req->rid))
761                 options |= BIT_4;
762         /* Use alternate PCI devfn */
763         if (LSB(req->rid))
764                 options |= BIT_5;
765         req->options = options;
766
767         ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
768             "options=0x%x.\n", req->options);
769         ql_dbg(ql_dbg_init, base_vha, 0x00dd,
770             "options=0x%x.\n", req->options);
771         for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
772                 req->outstanding_cmds[cnt] = NULL;
773         req->current_outstanding_cmd = 1;
774
775         req->ring_ptr = req->ring;
776         req->ring_index = 0;
777         req->cnt = req->length;
778         req->id = que_id;
779         reg = ISP_QUE_REG(ha, que_id);
780         req->req_q_in = &reg->isp25mq.req_q_in;
781         req->req_q_out = &reg->isp25mq.req_q_out;
782         req->max_q_depth = ha->req_q_map[0]->max_q_depth;
783         req->out_ptr = (uint16_t *)(req->ring + req->length);
784         mutex_unlock(&ha->mq_lock);
785         ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
786             "ring_ptr=%p ring_index=%d, "
787             "cnt=%d id=%d max_q_depth=%d.\n",
788             req->ring_ptr, req->ring_index,
789             req->cnt, req->id, req->max_q_depth);
790         ql_dbg(ql_dbg_init, base_vha, 0x00de,
791             "ring_ptr=%p ring_index=%d, "
792             "cnt=%d id=%d max_q_depth=%d.\n",
793             req->ring_ptr, req->ring_index, req->cnt,
794             req->id, req->max_q_depth);
795
796         if (startqp) {
797                 ret = qla25xx_init_req_que(base_vha, req);
798                 if (ret != QLA_SUCCESS) {
799                         ql_log(ql_log_fatal, base_vha, 0x00df,
800                             "%s failed.\n", __func__);
801                         mutex_lock(&ha->mq_lock);
802                         clear_bit(que_id, ha->req_qid_map);
803                         mutex_unlock(&ha->mq_lock);
804                         goto que_failed;
805                 }
806                 vha->flags.qpairs_req_created = 1;
807         }
808
809         return req->id;
810
811 que_failed:
812         qla25xx_free_req_que(base_vha, req);
813 failed:
814         return 0;
815 }
816
817 static void qla_do_work(struct work_struct *work)
818 {
819         unsigned long flags;
820         struct qla_qpair *qpair = container_of(work, struct qla_qpair, q_work);
821         struct scsi_qla_host *vha = qpair->vha;
822
823         spin_lock_irqsave(&qpair->qp_lock, flags);
824         qla24xx_process_response_queue(vha, qpair->rsp);
825         spin_unlock_irqrestore(&qpair->qp_lock, flags);
826
827 }
828
829 /* create response queue */
830 int
831 qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
832     uint8_t vp_idx, uint16_t rid, struct qla_qpair *qpair, bool startqp)
833 {
834         int ret = 0;
835         struct rsp_que *rsp = NULL;
836         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
837         struct scsi_qla_host *vha = pci_get_drvdata(ha->pdev);
838         uint16_t que_id = 0;
839         device_reg_t *reg;
840
841         rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
842         if (rsp == NULL) {
843                 ql_log(ql_log_warn, base_vha, 0x0066,
844                     "Failed to allocate memory for response queue.\n");
845                 goto failed;
846         }
847
848         rsp->length = RESPONSE_ENTRY_CNT_MQ;
849         rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
850                         (rsp->length + 1) * sizeof(response_t),
851                         &rsp->dma, GFP_KERNEL);
852         if (rsp->ring == NULL) {
853                 ql_log(ql_log_warn, base_vha, 0x00e1,
854                     "Failed to allocate memory for response ring.\n");
855                 goto que_failed;
856         }
857
858         mutex_lock(&ha->mq_lock);
859         que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
860         if (que_id >= ha->max_rsp_queues) {
861                 mutex_unlock(&ha->mq_lock);
862                 ql_log(ql_log_warn, base_vha, 0x00e2,
863                     "No resources to create additional request queue.\n");
864                 goto que_failed;
865         }
866         set_bit(que_id, ha->rsp_qid_map);
867
868         rsp->msix = qpair->msix;
869
870         ha->rsp_q_map[que_id] = rsp;
871         rsp->rid = rid;
872         rsp->vp_idx = vp_idx;
873         rsp->hw = ha;
874         ql_dbg(ql_dbg_init, base_vha, 0x00e4,
875             "rsp queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
876             que_id, rsp->rid, rsp->vp_idx, rsp->hw);
877         /* Use alternate PCI bus number */
878         if (MSB(rsp->rid))
879                 options |= BIT_4;
880         /* Use alternate PCI devfn */
881         if (LSB(rsp->rid))
882                 options |= BIT_5;
883         /* Enable MSIX handshake mode on for uncapable adapters */
884         if (!IS_MSIX_NACK_CAPABLE(ha))
885                 options |= BIT_6;
886
887         /* Set option to indicate response queue creation */
888         options |= BIT_1;
889
890         rsp->options = options;
891         rsp->id = que_id;
892         reg = ISP_QUE_REG(ha, que_id);
893         rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
894         rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
895         rsp->in_ptr = (uint16_t *)(rsp->ring + rsp->length);
896         mutex_unlock(&ha->mq_lock);
897         ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
898             "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
899             rsp->options, rsp->id, rsp->rsp_q_in,
900             rsp->rsp_q_out);
901         ql_dbg(ql_dbg_init, base_vha, 0x00e5,
902             "options=%x id=%d rsp_q_in=%p rsp_q_out=%p\n",
903             rsp->options, rsp->id, rsp->rsp_q_in,
904             rsp->rsp_q_out);
905
906         ret = qla25xx_request_irq(ha, qpair, qpair->msix,
907                 ha->flags.disable_msix_handshake ?
908                 QLA_MSIX_QPAIR_MULTIQ_RSP_Q : QLA_MSIX_QPAIR_MULTIQ_RSP_Q_HS);
909         if (ret)
910                 goto que_failed;
911
912         if (startqp) {
913                 ret = qla25xx_init_rsp_que(base_vha, rsp);
914                 if (ret != QLA_SUCCESS) {
915                         ql_log(ql_log_fatal, base_vha, 0x00e7,
916                             "%s failed.\n", __func__);
917                         mutex_lock(&ha->mq_lock);
918                         clear_bit(que_id, ha->rsp_qid_map);
919                         mutex_unlock(&ha->mq_lock);
920                         goto que_failed;
921                 }
922                 vha->flags.qpairs_rsp_created = 1;
923         }
924         rsp->req = NULL;
925
926         qla2x00_init_response_q_entries(rsp);
927         if (qpair->hw->wq)
928                 INIT_WORK(&qpair->q_work, qla_do_work);
929         return rsp->id;
930
931 que_failed:
932         qla25xx_free_rsp_que(base_vha, rsp);
933 failed:
934         return 0;
935 }
936
937 static void qla_ctrlvp_sp_done(srb_t *sp, int res)
938 {
939         if (sp->comp)
940                 complete(sp->comp);
941         /* don't free sp here. Let the caller do the free */
942 }
943
944 /**
945  * qla24xx_control_vp() - Enable a virtual port for given host
946  * @vha:        adapter block pointer
947  * @cmd:        command type to be sent for enable virtual port
948  *
949  * Return:      qla2xxx local function return status code.
950  */
951 int qla24xx_control_vp(scsi_qla_host_t *vha, int cmd)
952 {
953         int rval = QLA_MEMORY_ALLOC_FAILED;
954         struct qla_hw_data *ha = vha->hw;
955         int     vp_index = vha->vp_idx;
956         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
957         DECLARE_COMPLETION_ONSTACK(comp);
958         srb_t *sp;
959
960         ql_dbg(ql_dbg_vport, vha, 0x10c1,
961             "Entered %s cmd %x index %d.\n", __func__, cmd, vp_index);
962
963         if (vp_index == 0 || vp_index >= ha->max_npiv_vports)
964                 return QLA_PARAMETER_ERROR;
965
966         /* ref: INIT */
967         sp = qla2x00_get_sp(base_vha, NULL, GFP_KERNEL);
968         if (!sp)
969                 return rval;
970
971         sp->type = SRB_CTRL_VP;
972         sp->name = "ctrl_vp";
973         sp->comp = &comp;
974         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
975                               qla_ctrlvp_sp_done);
976         sp->u.iocb_cmd.u.ctrlvp.cmd = cmd;
977         sp->u.iocb_cmd.u.ctrlvp.vp_index = vp_index;
978
979         rval = qla2x00_start_sp(sp);
980         if (rval != QLA_SUCCESS) {
981                 ql_dbg(ql_dbg_async, vha, 0xffff,
982                     "%s: %s Failed submission. %x.\n",
983                     __func__, sp->name, rval);
984                 goto done;
985         }
986
987         ql_dbg(ql_dbg_vport, vha, 0x113f, "%s hndl %x submitted\n",
988             sp->name, sp->handle);
989
990         wait_for_completion(&comp);
991         sp->comp = NULL;
992
993         rval = sp->rc;
994         switch (rval) {
995         case QLA_FUNCTION_TIMEOUT:
996                 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Timeout. %x.\n",
997                     __func__, sp->name, rval);
998                 break;
999         case QLA_SUCCESS:
1000                 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s done.\n",
1001                     __func__, sp->name);
1002                 break;
1003         default:
1004                 ql_dbg(ql_dbg_vport, vha, 0xffff, "%s: %s Failed. %x.\n",
1005                     __func__, sp->name, rval);
1006                 break;
1007         }
1008 done:
1009         /* ref: INIT */
1010         kref_put(&sp->cmd_kref, qla2x00_sp_release);
1011         return rval;
1012 }