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