Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[sfrench/cifs-2.6.git] / drivers / scsi / qla2xxx / qla_mid.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_gbl.h"
9
10 #include <linux/moduleparam.h>
11 #include <linux/vmalloc.h>
12 #include <linux/smp_lock.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
34         /* Find an empty slot and assign an vp_id */
35         mutex_lock(&ha->vport_lock);
36         vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
37         if (vp_id > ha->max_npiv_vports) {
38                 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
39                     vp_id, ha->max_npiv_vports));
40                 mutex_unlock(&ha->vport_lock);
41                 return vp_id;
42         }
43
44         set_bit(vp_id, ha->vp_idx_map);
45         ha->num_vhosts++;
46         ha->cur_vport_count++;
47         vha->vp_idx = vp_id;
48         list_add_tail(&vha->list, &ha->vp_list);
49         mutex_unlock(&ha->vport_lock);
50         return vp_id;
51 }
52
53 void
54 qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
55 {
56         uint16_t vp_id;
57         struct qla_hw_data *ha = vha->hw;
58
59         mutex_lock(&ha->vport_lock);
60         vp_id = vha->vp_idx;
61         ha->num_vhosts--;
62         ha->cur_vport_count--;
63         clear_bit(vp_id, ha->vp_idx_map);
64         list_del(&vha->list);
65         mutex_unlock(&ha->vport_lock);
66 }
67
68 static scsi_qla_host_t *
69 qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
70 {
71         scsi_qla_host_t *vha;
72
73         /* Locate matching device in database. */
74         list_for_each_entry(vha, &ha->vp_list, list) {
75                 if (!memcmp(port_name, vha->port_name, WWN_SIZE))
76                         return vha;
77         }
78         return NULL;
79 }
80
81 /*
82  * qla2x00_mark_vp_devices_dead
83  *      Updates fcport state when device goes offline.
84  *
85  * Input:
86  *      ha = adapter block pointer.
87  *      fcport = port structure pointer.
88  *
89  * Return:
90  *      None.
91  *
92  * Context:
93  */
94 static void
95 qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
96 {
97         fc_port_t *fcport;
98
99         list_for_each_entry(fcport, &vha->vp_fcports, list) {
100                 DEBUG15(printk("scsi(%ld): Marking port dead, "
101                     "loop_id=0x%04x :%x\n",
102                     vha->host_no, fcport->loop_id, fcport->vp_idx));
103
104                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
105                 qla2x00_mark_device_lost(vha, fcport, 0, 0);
106                 atomic_set(&fcport->state, FCS_UNCONFIGURED);
107         }
108 }
109
110 int
111 qla24xx_disable_vp(scsi_qla_host_t *vha)
112 {
113         int ret;
114
115         ret = qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
116         atomic_set(&vha->loop_state, LOOP_DOWN);
117         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
118
119         qla2x00_mark_vp_devices_dead(vha);
120         atomic_set(&vha->vp_state, VP_FAILED);
121         vha->flags.management_server_logged_in = 0;
122         if (ret == QLA_SUCCESS) {
123                 fc_vport_set_state(vha->fc_vport, FC_VPORT_DISABLED);
124         } else {
125                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
126                 return -1;
127         }
128         return 0;
129 }
130
131 int
132 qla24xx_enable_vp(scsi_qla_host_t *vha)
133 {
134         int ret;
135         struct qla_hw_data *ha = vha->hw;
136         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
137
138         /* Check if physical ha port is Up */
139         if (atomic_read(&base_vha->loop_state) == LOOP_DOWN  ||
140                 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
141                 vha->vp_err_state =  VP_ERR_PORTDWN;
142                 fc_vport_set_state(vha->fc_vport, FC_VPORT_LINKDOWN);
143                 goto enable_failed;
144         }
145
146         /* Initialize the new vport unless it is a persistent port */
147         mutex_lock(&ha->vport_lock);
148         ret = qla24xx_modify_vp_config(vha);
149         mutex_unlock(&ha->vport_lock);
150
151         if (ret != QLA_SUCCESS) {
152                 fc_vport_set_state(vha->fc_vport, FC_VPORT_FAILED);
153                 goto enable_failed;
154         }
155
156         DEBUG15(qla_printk(KERN_INFO, ha,
157             "Virtual port with id: %d - Enabled\n", vha->vp_idx));
158         return 0;
159
160 enable_failed:
161         DEBUG15(qla_printk(KERN_INFO, ha,
162             "Virtual port with id: %d - Disabled\n", vha->vp_idx));
163         return 1;
164 }
165
166 static void
167 qla24xx_configure_vp(scsi_qla_host_t *vha)
168 {
169         struct fc_vport *fc_vport;
170         int ret;
171
172         fc_vport = vha->fc_vport;
173
174         DEBUG15(printk("scsi(%ld): %s: change request #3 for this host.\n",
175             vha->host_no, __func__));
176         ret = qla2x00_send_change_request(vha, 0x3, vha->vp_idx);
177         if (ret != QLA_SUCCESS) {
178                 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
179                     "receiving of RSCN requests: 0x%x\n", ret));
180                 return;
181         } else {
182                 /* Corresponds to SCR enabled */
183                 clear_bit(VP_SCR_NEEDED, &vha->vp_flags);
184         }
185
186         vha->flags.online = 1;
187         if (qla24xx_configure_vhba(vha))
188                 return;
189
190         atomic_set(&vha->vp_state, VP_ACTIVE);
191         fc_vport_set_state(fc_vport, FC_VPORT_ACTIVE);
192 }
193
194 void
195 qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
196 {
197         scsi_qla_host_t *vha;
198         struct qla_hw_data *ha = rsp->hw;
199         int i = 0;
200
201         list_for_each_entry(vha, &ha->vp_list, list) {
202                 if (vha->vp_idx) {
203                         switch (mb[0]) {
204                         case MBA_LIP_OCCURRED:
205                         case MBA_LOOP_UP:
206                         case MBA_LOOP_DOWN:
207                         case MBA_LIP_RESET:
208                         case MBA_POINT_TO_POINT:
209                         case MBA_CHG_IN_CONNECTION:
210                         case MBA_PORT_UPDATE:
211                         case MBA_RSCN_UPDATE:
212                                 DEBUG15(printk("scsi(%ld)%s: Async_event for"
213                                 " VP[%d], mb = 0x%x, vha=%p\n",
214                                 vha->host_no, __func__, i, *mb, vha));
215                                 qla2x00_async_event(vha, rsp, mb);
216                                 break;
217                         }
218                 }
219                 i++;
220         }
221 }
222
223 int
224 qla2x00_vp_abort_isp(scsi_qla_host_t *vha)
225 {
226         /*
227          * Physical port will do most of the abort and recovery work. We can
228          * just treat it as a loop down
229          */
230         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
231                 atomic_set(&vha->loop_state, LOOP_DOWN);
232                 qla2x00_mark_all_devices_lost(vha, 0);
233         } else {
234                 if (!atomic_read(&vha->loop_down_timer))
235                         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
236         }
237
238         /* To exclusively reset vport, we need to log it out first.*/
239         if (!test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
240                 qla24xx_control_vp(vha, VCE_COMMAND_DISABLE_VPS_LOGO_ALL);
241
242         DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
243             vha->host_no, vha->vp_idx));
244         return qla24xx_enable_vp(vha);
245 }
246
247 static int
248 qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
249 {
250         struct qla_hw_data *ha = vha->hw;
251         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
252
253         if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
254                 /* VP acquired. complete port configuration */
255                 if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
256                         qla24xx_configure_vp(vha);
257                 } else {
258                         set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
259                         set_bit(VP_DPC_NEEDED, &base_vha->dpc_flags);
260                 }
261
262                 return 0;
263         }
264
265         if (test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags)) {
266                 qla2x00_update_fcports(vha);
267                 clear_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
268         }
269
270         if ((test_and_clear_bit(RELOGIN_NEEDED, &vha->dpc_flags)) &&
271                 !test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) &&
272                 atomic_read(&vha->loop_state) != LOOP_DOWN) {
273
274                 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
275                                                 vha->host_no));
276                 qla2x00_relogin(vha);
277
278                 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
279                                                         vha->host_no));
280         }
281
282         if (test_and_clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) &&
283             (!(test_and_set_bit(RESET_ACTIVE, &vha->dpc_flags)))) {
284                 clear_bit(RESET_ACTIVE, &vha->dpc_flags);
285         }
286
287         if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
288                 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags))) {
289                         qla2x00_loop_resync(vha);
290                         clear_bit(LOOP_RESYNC_ACTIVE, &vha->dpc_flags);
291                 }
292         }
293
294         return 0;
295 }
296
297 void
298 qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
299 {
300         int ret;
301         struct qla_hw_data *ha = vha->hw;
302         scsi_qla_host_t *vp;
303
304         if (vha->vp_idx)
305                 return;
306         if (list_empty(&ha->vp_list))
307                 return;
308
309         clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
310
311         list_for_each_entry(vp, &ha->vp_list, list) {
312                 if (vp->vp_idx)
313                         ret = qla2x00_do_dpc_vp(vp);
314         }
315 }
316
317 int
318 qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
319 {
320         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
321         struct qla_hw_data *ha = base_vha->hw;
322         scsi_qla_host_t *vha;
323         uint8_t port_name[WWN_SIZE];
324
325         if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
326                 return VPCERR_UNSUPPORTED;
327
328         /* Check up the F/W and H/W support NPIV */
329         if (!ha->flags.npiv_supported)
330                 return VPCERR_UNSUPPORTED;
331
332         /* Check up whether npiv supported switch presented */
333         if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
334                 return VPCERR_NO_FABRIC_SUPP;
335
336         /* Check up unique WWPN */
337         u64_to_wwn(fc_vport->port_name, port_name);
338         if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
339                 return VPCERR_BAD_WWN;
340         vha = qla24xx_find_vhost_by_name(ha, port_name);
341         if (vha)
342                 return VPCERR_BAD_WWN;
343
344         /* Check up max-npiv-supports */
345         if (ha->num_vhosts > ha->max_npiv_vports) {
346                 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
347                     "max_npv_vports %ud.\n", base_vha->host_no,
348                     ha->num_vhosts, ha->max_npiv_vports));
349                 return VPCERR_UNSUPPORTED;
350         }
351         return 0;
352 }
353
354 scsi_qla_host_t *
355 qla24xx_create_vhost(struct fc_vport *fc_vport)
356 {
357         scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
358         struct qla_hw_data *ha = base_vha->hw;
359         scsi_qla_host_t *vha;
360         struct scsi_host_template *sht = &qla24xx_driver_template;
361         struct Scsi_Host *host;
362
363         vha = qla2x00_create_host(sht, ha);
364         if (!vha) {
365                 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
366                 return(NULL);
367         }
368
369         host = vha->host;
370         fc_vport->dd_data = vha;
371         /* New host info */
372         u64_to_wwn(fc_vport->node_name, vha->node_name);
373         u64_to_wwn(fc_vport->port_name, vha->port_name);
374
375         vha->fc_vport = fc_vport;
376         vha->device_flags = 0;
377         vha->vp_idx = qla24xx_allocate_vp_id(vha);
378         if (vha->vp_idx > ha->max_npiv_vports) {
379                 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
380                         vha->host_no));
381                 goto create_vhost_failed;
382         }
383         vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
384
385         vha->dpc_flags = 0L;
386         set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
387         set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
388
389         /*
390          * To fix the issue of processing a parent's RSCN for the vport before
391          * its SCR is complete.
392          */
393         set_bit(VP_SCR_NEEDED, &vha->vp_flags);
394         atomic_set(&vha->loop_state, LOOP_DOWN);
395         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
396
397         qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
398
399         memset(vha->req_ques, 0, sizeof(vha->req_ques) * QLA_MAX_HOST_QUES);
400         vha->req_ques[0] = ha->req_q_map[0]->id;
401         host->can_queue = ha->req_q_map[0]->length + 128;
402         host->this_id = 255;
403         host->cmd_per_lun = 3;
404         host->max_cmd_len = MAX_CMDSZ;
405         host->max_channel = MAX_BUSES - 1;
406         host->max_lun = MAX_LUNS;
407         host->unique_id = host->host_no;
408         host->max_id = MAX_TARGETS_2200;
409         host->transportt = qla2xxx_transport_vport_template;
410
411         DEBUG15(printk("DEBUG: detect vport hba %ld at address = %p\n",
412             vha->host_no, vha));
413
414         vha->flags.init_done = 1;
415
416         return vha;
417
418 create_vhost_failed:
419         return NULL;
420 }
421
422 static void
423 qla25xx_free_req_que(struct scsi_qla_host *vha, struct req_que *req)
424 {
425         struct qla_hw_data *ha = vha->hw;
426         uint16_t que_id = req->id;
427
428         dma_free_coherent(&ha->pdev->dev, (req->length + 1) *
429                 sizeof(request_t), req->ring, req->dma);
430         req->ring = NULL;
431         req->dma = 0;
432         if (que_id) {
433                 ha->req_q_map[que_id] = NULL;
434                 mutex_lock(&ha->vport_lock);
435                 clear_bit(que_id, ha->req_qid_map);
436                 mutex_unlock(&ha->vport_lock);
437         }
438         kfree(req);
439         req = NULL;
440 }
441
442 static void
443 qla25xx_free_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
444 {
445         struct qla_hw_data *ha = vha->hw;
446         uint16_t que_id = rsp->id;
447
448         if (rsp->msix && rsp->msix->have_irq) {
449                 free_irq(rsp->msix->vector, rsp);
450                 rsp->msix->have_irq = 0;
451                 rsp->msix->rsp = NULL;
452         }
453         dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
454                 sizeof(response_t), rsp->ring, rsp->dma);
455         rsp->ring = NULL;
456         rsp->dma = 0;
457         if (que_id) {
458                 ha->rsp_q_map[que_id] = NULL;
459                 mutex_lock(&ha->vport_lock);
460                 clear_bit(que_id, ha->rsp_qid_map);
461                 mutex_unlock(&ha->vport_lock);
462         }
463         kfree(rsp);
464         rsp = NULL;
465 }
466
467 int
468 qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
469 {
470         int ret = -1;
471
472         if (req) {
473                 req->options |= BIT_0;
474                 ret = qla25xx_init_req_que(vha, req, req->options);
475         }
476         if (ret == QLA_SUCCESS)
477                 qla25xx_free_req_que(vha, req);
478
479         return ret;
480 }
481
482 int
483 qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
484 {
485         int ret = -1;
486
487         if (rsp) {
488                 rsp->options |= BIT_0;
489                 ret = qla25xx_init_rsp_que(vha, rsp, rsp->options);
490         }
491         if (ret == QLA_SUCCESS)
492                 qla25xx_free_rsp_que(vha, rsp);
493
494         return ret;
495 }
496
497 int qla25xx_update_req_que(struct scsi_qla_host *vha, uint8_t que, uint8_t qos)
498 {
499         int ret = 0;
500         struct qla_hw_data *ha = vha->hw;
501         struct req_que *req = ha->req_q_map[que];
502
503         req->options |= BIT_3;
504         req->qos = qos;
505         ret = qla25xx_init_req_que(vha, req, req->options);
506         if (ret != QLA_SUCCESS)
507                 DEBUG2_17(printk(KERN_WARNING "%s failed\n", __func__));
508         /* restore options bit */
509         req->options &= ~BIT_3;
510         return ret;
511 }
512
513
514 /* Delete all queues for a given vhost */
515 int
516 qla25xx_delete_queues(struct scsi_qla_host *vha, uint8_t que_no)
517 {
518         int cnt, ret = 0;
519         struct req_que *req = NULL;
520         struct rsp_que *rsp = NULL;
521         struct qla_hw_data *ha = vha->hw;
522
523         if (que_no) {
524         /* Delete request queue */
525                 req = ha->req_q_map[que_no];
526                 if (req) {
527                         rsp = req->rsp;
528                         ret = qla25xx_delete_req_que(vha, req);
529                         if (ret != QLA_SUCCESS) {
530                                 qla_printk(KERN_WARNING, ha,
531                                 "Couldn't delete req que %d\n", req->id);
532                                 return ret;
533                         }
534                         /* Delete associated response queue */
535                         if (rsp) {
536                                 ret = qla25xx_delete_rsp_que(vha, rsp);
537                                 if (ret != QLA_SUCCESS) {
538                                         qla_printk(KERN_WARNING, ha,
539                                                 "Couldn't delete rsp que %d\n",
540                                                 rsp->id);
541                                         return ret;
542                                 }
543                         }
544                 }
545         } else {  /* delete all queues of this host */
546                 for (cnt = 0; cnt < QLA_MAX_HOST_QUES; cnt++) {
547                         /* Delete request queues */
548                         req = ha->req_q_map[vha->req_ques[cnt]];
549                         if (req && req->id) {
550                                 rsp = req->rsp;
551                                 ret = qla25xx_delete_req_que(vha, req);
552                                 if (ret != QLA_SUCCESS) {
553                                         qla_printk(KERN_WARNING, ha,
554                                                 "Couldn't delete req que %d\n",
555                                                 vha->req_ques[cnt]);
556                                         return ret;
557                                 }
558                                 vha->req_ques[cnt] = ha->req_q_map[0]->id;
559                         /* Delete associated response queue */
560                                 if (rsp && rsp->id) {
561                                         ret = qla25xx_delete_rsp_que(vha, rsp);
562                                         if (ret != QLA_SUCCESS) {
563                                                 qla_printk(KERN_WARNING, ha,
564                                                 "Couldn't delete rsp que %d\n",
565                                                 rsp->id);
566                                                 return ret;
567                                         }
568                                 }
569                         }
570                 }
571         }
572         qla_printk(KERN_INFO, ha, "Queues deleted for vport:%d\n",
573                 vha->vp_idx);
574         return ret;
575 }
576
577 int
578 qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
579         uint8_t vp_idx, uint16_t rid, uint8_t rsp_que, uint8_t qos)
580 {
581         int ret = 0;
582         struct req_que *req = NULL;
583         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
584         uint16_t que_id = 0;
585
586         req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
587         if (req == NULL) {
588                 qla_printk(KERN_WARNING, ha, "could not allocate memory"
589                         "for request que\n");
590                 goto que_failed;
591         }
592
593         req->length = REQUEST_ENTRY_CNT_24XX;
594         req->ring = dma_alloc_coherent(&ha->pdev->dev,
595                         (req->length + 1) * sizeof(request_t),
596                         &req->dma, GFP_KERNEL);
597         if (req->ring == NULL) {
598                 qla_printk(KERN_WARNING, ha,
599                 "Memory Allocation failed - request_ring\n");
600                 goto que_failed;
601         }
602
603         mutex_lock(&ha->vport_lock);
604         que_id = find_first_zero_bit(ha->req_qid_map, ha->max_queues);
605         if (que_id >= ha->max_queues) {
606                 mutex_unlock(&ha->vport_lock);
607                 qla_printk(KERN_INFO, ha, "No resources to create "
608                          "additional request queue\n");
609                 goto que_failed;
610         }
611         set_bit(que_id, ha->req_qid_map);
612         ha->req_q_map[que_id] = req;
613         req->rid = rid;
614         req->vp_idx = vp_idx;
615         req->qos = qos;
616
617         if (ha->rsp_q_map[rsp_que]) {
618                 req->rsp = ha->rsp_q_map[rsp_que];
619                 req->rsp->req = req;
620         }
621         /* Use alternate PCI bus number */
622         if (MSB(req->rid))
623                 options |= BIT_4;
624         /* Use alternate PCI devfn */
625         if (LSB(req->rid))
626                 options |= BIT_5;
627         req->options = options;
628         req->ring_ptr = req->ring;
629         req->ring_index = 0;
630         req->cnt = req->length;
631         req->id = que_id;
632         req->max_q_depth = ha->req_q_map[0]->max_q_depth;
633         mutex_unlock(&ha->vport_lock);
634
635         ret = qla25xx_init_req_que(base_vha, req, options);
636         if (ret != QLA_SUCCESS) {
637                 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
638                 mutex_lock(&ha->vport_lock);
639                 clear_bit(que_id, ha->req_qid_map);
640                 mutex_unlock(&ha->vport_lock);
641                 goto que_failed;
642         }
643
644         return req->id;
645
646 que_failed:
647         qla25xx_free_req_que(base_vha, req);
648         return 0;
649 }
650
651 /* create response queue */
652 int
653 qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
654         uint8_t vp_idx, uint16_t rid)
655 {
656         int ret = 0;
657         struct rsp_que *rsp = NULL;
658         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
659         uint16_t que_id = 0;;
660
661         rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
662         if (rsp == NULL) {
663                 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
664                                 " response que\n");
665                 goto que_failed;
666         }
667
668         rsp->length = RESPONSE_ENTRY_CNT_2300;
669         rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
670                         (rsp->length + 1) * sizeof(response_t),
671                         &rsp->dma, GFP_KERNEL);
672         if (rsp->ring == NULL) {
673                 qla_printk(KERN_WARNING, ha,
674                 "Memory Allocation failed - response_ring\n");
675                 goto que_failed;
676         }
677
678         mutex_lock(&ha->vport_lock);
679         que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_queues);
680         if (que_id >= ha->max_queues) {
681                 mutex_unlock(&ha->vport_lock);
682                 qla_printk(KERN_INFO, ha, "No resources to create "
683                          "additional response queue\n");
684                 goto que_failed;
685         }
686         set_bit(que_id, ha->rsp_qid_map);
687
688         if (ha->flags.msix_enabled)
689                 rsp->msix = &ha->msix_entries[que_id + 1];
690         else
691                 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
692
693         ha->rsp_q_map[que_id] = rsp;
694         rsp->rid = rid;
695         rsp->vp_idx = vp_idx;
696         rsp->hw = ha;
697         /* Use alternate PCI bus number */
698         if (MSB(rsp->rid))
699                 options |= BIT_4;
700         /* Use alternate PCI devfn */
701         if (LSB(rsp->rid))
702                 options |= BIT_5;
703         rsp->options = options;
704         rsp->ring_ptr = rsp->ring;
705         rsp->ring_index = 0;
706         rsp->id = que_id;
707         mutex_unlock(&ha->vport_lock);
708
709         ret = qla25xx_request_irq(rsp);
710         if (ret)
711                 goto que_failed;
712
713         ret = qla25xx_init_rsp_que(base_vha, rsp, options);
714         if (ret != QLA_SUCCESS) {
715                 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
716                 mutex_lock(&ha->vport_lock);
717                 clear_bit(que_id, ha->rsp_qid_map);
718                 mutex_unlock(&ha->vport_lock);
719                 goto que_failed;
720         }
721
722         qla2x00_init_response_q_entries(rsp);
723
724         return rsp->id;
725
726 que_failed:
727         qla25xx_free_rsp_que(base_vha, rsp);
728         return 0;
729 }
730
731 int
732 qla25xx_create_queues(struct scsi_qla_host *vha, uint8_t qos)
733 {
734         uint16_t options = 0;
735         uint8_t ret = 0;
736         struct qla_hw_data *ha = vha->hw;
737
738         options |= BIT_1;
739         ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0);
740         if (!ret) {
741                 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
742                 return ret;
743         } else
744                 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
745
746         options = 0;
747         if (qos & BIT_7)
748                 options |= BIT_8;
749         ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
750                                         qos & ~BIT_7);
751         if (ret) {
752                 vha->req_ques[0] = ret;
753                 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
754         } else
755                 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
756
757         return ret;
758 }