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