GNU Linux-libre 4.9.292-gnu1
[releases.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);
546                 rsp->msix->have_irq = 0;
547                 rsp->msix->rsp = NULL;
548         }
549         dma_free_coherent(&ha->pdev->dev, (rsp->length + 1) *
550                 sizeof(response_t), rsp->ring, rsp->dma);
551         rsp->ring = NULL;
552         rsp->dma = 0;
553         if (que_id) {
554                 ha->rsp_q_map[que_id] = NULL;
555                 mutex_lock(&ha->vport_lock);
556                 clear_bit(que_id, ha->rsp_qid_map);
557                 mutex_unlock(&ha->vport_lock);
558         }
559         kfree(rsp);
560         rsp = NULL;
561 }
562
563 int
564 qla25xx_delete_req_que(struct scsi_qla_host *vha, struct req_que *req)
565 {
566         int ret = -1;
567
568         if (req) {
569                 req->options |= BIT_0;
570                 ret = qla25xx_init_req_que(vha, req);
571         }
572         if (ret == QLA_SUCCESS)
573                 qla25xx_free_req_que(vha, req);
574
575         return ret;
576 }
577
578 static int
579 qla25xx_delete_rsp_que(struct scsi_qla_host *vha, struct rsp_que *rsp)
580 {
581         int ret = -1;
582
583         if (rsp) {
584                 rsp->options |= BIT_0;
585                 ret = qla25xx_init_rsp_que(vha, rsp);
586         }
587         if (ret == QLA_SUCCESS)
588                 qla25xx_free_rsp_que(vha, rsp);
589
590         return ret;
591 }
592
593 /* Delete all queues for a given vhost */
594 int
595 qla25xx_delete_queues(struct scsi_qla_host *vha)
596 {
597         int cnt, ret = 0;
598         struct req_que *req = NULL;
599         struct rsp_que *rsp = NULL;
600         struct qla_hw_data *ha = vha->hw;
601
602         /* Delete request queues */
603         for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
604                 req = ha->req_q_map[cnt];
605                 if (req && test_bit(cnt, ha->req_qid_map)) {
606                         ret = qla25xx_delete_req_que(vha, req);
607                         if (ret != QLA_SUCCESS) {
608                                 ql_log(ql_log_warn, vha, 0x00ea,
609                                     "Couldn't delete req que %d.\n",
610                                     req->id);
611                                 return ret;
612                         }
613                 }
614         }
615
616         /* Delete response queues */
617         for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
618                 rsp = ha->rsp_q_map[cnt];
619                 if (rsp && test_bit(cnt, ha->rsp_qid_map)) {
620                         ret = qla25xx_delete_rsp_que(vha, rsp);
621                         if (ret != QLA_SUCCESS) {
622                                 ql_log(ql_log_warn, vha, 0x00eb,
623                                     "Couldn't delete rsp que %d.\n",
624                                     rsp->id);
625                                 return ret;
626                         }
627                 }
628         }
629         return ret;
630 }
631
632 int
633 qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
634         uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
635 {
636         int ret = 0;
637         struct req_que *req = NULL;
638         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
639         uint16_t que_id = 0;
640         device_reg_t *reg;
641         uint32_t cnt;
642
643         req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
644         if (req == NULL) {
645                 ql_log(ql_log_fatal, base_vha, 0x00d9,
646                     "Failed to allocate memory for request queue.\n");
647                 goto failed;
648         }
649
650         req->length = REQUEST_ENTRY_CNT_24XX;
651         req->ring = dma_alloc_coherent(&ha->pdev->dev,
652                         (req->length + 1) * sizeof(request_t),
653                         &req->dma, GFP_KERNEL);
654         if (req->ring == NULL) {
655                 ql_log(ql_log_fatal, base_vha, 0x00da,
656                     "Failed to allocate memory for request_ring.\n");
657                 goto que_failed;
658         }
659
660         ret = qla2x00_alloc_outstanding_cmds(ha, req);
661         if (ret != QLA_SUCCESS)
662                 goto que_failed;
663
664         mutex_lock(&ha->vport_lock);
665         que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
666         if (que_id >= ha->max_req_queues) {
667                 mutex_unlock(&ha->vport_lock);
668                 ql_log(ql_log_warn, base_vha, 0x00db,
669                     "No resources to create additional request queue.\n");
670                 goto que_failed;
671         }
672         set_bit(que_id, ha->req_qid_map);
673         ha->req_q_map[que_id] = req;
674         req->rid = rid;
675         req->vp_idx = vp_idx;
676         req->qos = qos;
677
678         ql_dbg(ql_dbg_multiq, base_vha, 0xc002,
679             "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
680             que_id, req->rid, req->vp_idx, req->qos);
681         ql_dbg(ql_dbg_init, base_vha, 0x00dc,
682             "queue_id=%d rid=%d vp_idx=%d qos=%d.\n",
683             que_id, req->rid, req->vp_idx, req->qos);
684         if (rsp_que < 0)
685                 req->rsp = NULL;
686         else
687                 req->rsp = ha->rsp_q_map[rsp_que];
688         /* Use alternate PCI bus number */
689         if (MSB(req->rid))
690                 options |= BIT_4;
691         /* Use alternate PCI devfn */
692         if (LSB(req->rid))
693                 options |= BIT_5;
694         req->options = options;
695
696         ql_dbg(ql_dbg_multiq, base_vha, 0xc003,
697             "options=0x%x.\n", req->options);
698         ql_dbg(ql_dbg_init, base_vha, 0x00dd,
699             "options=0x%x.\n", req->options);
700         for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
701                 req->outstanding_cmds[cnt] = NULL;
702         req->current_outstanding_cmd = 1;
703
704         req->ring_ptr = req->ring;
705         req->ring_index = 0;
706         req->cnt = req->length;
707         req->id = que_id;
708         reg = ISP_QUE_REG(ha, que_id);
709         req->req_q_in = &reg->isp25mq.req_q_in;
710         req->req_q_out = &reg->isp25mq.req_q_out;
711         req->max_q_depth = ha->req_q_map[0]->max_q_depth;
712         req->out_ptr = (void *)(req->ring + req->length);
713         mutex_unlock(&ha->vport_lock);
714         ql_dbg(ql_dbg_multiq, base_vha, 0xc004,
715             "ring_ptr=%p ring_index=%d, "
716             "cnt=%d id=%d max_q_depth=%d.\n",
717             req->ring_ptr, req->ring_index,
718             req->cnt, req->id, req->max_q_depth);
719         ql_dbg(ql_dbg_init, base_vha, 0x00de,
720             "ring_ptr=%p ring_index=%d, "
721             "cnt=%d id=%d max_q_depth=%d.\n",
722             req->ring_ptr, req->ring_index, req->cnt,
723             req->id, req->max_q_depth);
724
725         ret = qla25xx_init_req_que(base_vha, req);
726         if (ret != QLA_SUCCESS) {
727                 ql_log(ql_log_fatal, base_vha, 0x00df,
728                     "%s failed.\n", __func__);
729                 mutex_lock(&ha->vport_lock);
730                 clear_bit(que_id, ha->req_qid_map);
731                 mutex_unlock(&ha->vport_lock);
732                 goto que_failed;
733         }
734
735         return req->id;
736
737 que_failed:
738         qla25xx_free_req_que(base_vha, req);
739 failed:
740         return 0;
741 }
742
743 static void qla_do_work(struct work_struct *work)
744 {
745         unsigned long flags;
746         struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
747         struct scsi_qla_host *vha;
748         struct qla_hw_data *ha = rsp->hw;
749
750         spin_lock_irqsave(&rsp->hw->hardware_lock, flags);
751         vha = pci_get_drvdata(ha->pdev);
752         qla24xx_process_response_queue(vha, rsp);
753         spin_unlock_irqrestore(&rsp->hw->hardware_lock, flags);
754 }
755
756 /* create response queue */
757 int
758 qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
759         uint8_t vp_idx, uint16_t rid, int req)
760 {
761         int ret = 0;
762         struct rsp_que *rsp = NULL;
763         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
764         uint16_t que_id = 0;
765         device_reg_t *reg;
766
767         rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
768         if (rsp == NULL) {
769                 ql_log(ql_log_warn, base_vha, 0x0066,
770                     "Failed to allocate memory for response queue.\n");
771                 goto failed;
772         }
773
774         rsp->length = RESPONSE_ENTRY_CNT_MQ;
775         rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
776                         (rsp->length + 1) * sizeof(response_t),
777                         &rsp->dma, GFP_KERNEL);
778         if (rsp->ring == NULL) {
779                 ql_log(ql_log_warn, base_vha, 0x00e1,
780                     "Failed to allocate memory for response ring.\n");
781                 goto que_failed;
782         }
783
784         mutex_lock(&ha->vport_lock);
785         que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
786         if (que_id >= ha->max_rsp_queues) {
787                 mutex_unlock(&ha->vport_lock);
788                 ql_log(ql_log_warn, base_vha, 0x00e2,
789                     "No resources to create additional request queue.\n");
790                 goto que_failed;
791         }
792         set_bit(que_id, ha->rsp_qid_map);
793
794         if (ha->flags.msix_enabled)
795                 rsp->msix = &ha->msix_entries[que_id + 1];
796         else
797                 ql_log(ql_log_warn, base_vha, 0x00e3,
798                     "MSIX not enabled.\n");
799
800         ha->rsp_q_map[que_id] = rsp;
801         rsp->rid = rid;
802         rsp->vp_idx = vp_idx;
803         rsp->hw = ha;
804         ql_dbg(ql_dbg_init, base_vha, 0x00e4,
805             "queue_id=%d rid=%d vp_idx=%d hw=%p.\n",
806             que_id, rsp->rid, rsp->vp_idx, rsp->hw);
807         /* Use alternate PCI bus number */
808         if (MSB(rsp->rid))
809                 options |= BIT_4;
810         /* Use alternate PCI devfn */
811         if (LSB(rsp->rid))
812                 options |= BIT_5;
813         /* Enable MSIX handshake mode on for uncapable adapters */
814         if (!IS_MSIX_NACK_CAPABLE(ha))
815                 options |= BIT_6;
816
817         rsp->options = options;
818         rsp->id = que_id;
819         reg = ISP_QUE_REG(ha, que_id);
820         rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
821         rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
822         rsp->in_ptr = (void *)(rsp->ring + rsp->length);
823         mutex_unlock(&ha->vport_lock);
824         ql_dbg(ql_dbg_multiq, base_vha, 0xc00b,
825             "options=%x id=%d rsp_q_in=%p rsp_q_out=%p",
826             rsp->options, rsp->id, rsp->rsp_q_in,
827             rsp->rsp_q_out);
828         ql_dbg(ql_dbg_init, base_vha, 0x00e5,
829             "options=%x id=%d rsp_q_in=%p rsp_q_out=%p",
830             rsp->options, rsp->id, rsp->rsp_q_in,
831             rsp->rsp_q_out);
832
833         ret = qla25xx_request_irq(rsp);
834         if (ret)
835                 goto que_failed;
836
837         ret = qla25xx_init_rsp_que(base_vha, rsp);
838         if (ret != QLA_SUCCESS) {
839                 ql_log(ql_log_fatal, base_vha, 0x00e7,
840                     "%s failed.\n", __func__);
841                 mutex_lock(&ha->vport_lock);
842                 clear_bit(que_id, ha->rsp_qid_map);
843                 mutex_unlock(&ha->vport_lock);
844                 goto que_failed;
845         }
846         if (req >= 0)
847                 rsp->req = ha->req_q_map[req];
848         else
849                 rsp->req = NULL;
850
851         qla2x00_init_response_q_entries(rsp);
852         if (rsp->hw->wq)
853                 INIT_WORK(&rsp->q_work, qla_do_work);
854         return rsp->id;
855
856 que_failed:
857         qla25xx_free_rsp_que(base_vha, rsp);
858 failed:
859         return 0;
860 }