]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/qla2xxx/qla_mid.c
headers: smp_lock.h redux
[net-next-2.6.git] / drivers / scsi / qla2xxx / qla_mid.c
CommitLineData
2c3dfe3f 1/*
01e58d8e
AV
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
2c3dfe3f 4 *
01e58d8e 5 * See LICENSE.qla2xxx for copyright and licensing details.
2c3dfe3f
SJ
6 */
7#include "qla_def.h"
7b867cf7 8#include "qla_gbl.h"
2c3dfe3f 9
2c3dfe3f
SJ
10#include <linux/moduleparam.h>
11#include <linux/vmalloc.h>
2c3dfe3f
SJ
12#include <linux/list.h>
13
14#include <scsi/scsi_tcq.h>
15#include <scsi/scsicam.h>
16#include <linux/delay.h>
17
2c3dfe3f
SJ
18void
19qla2x00_vp_stop_timer(scsi_qla_host_t *vha)
20{
7b867cf7 21 if (vha->vp_idx && vha->timer_active) {
2c3dfe3f
SJ
22 del_timer_sync(&vha->timer);
23 vha->timer_active = 0;
24 }
25}
26
a824ebb3 27static uint32_t
2c3dfe3f
SJ
28qla24xx_allocate_vp_id(scsi_qla_host_t *vha)
29{
30 uint32_t vp_id;
7b867cf7 31 struct qla_hw_data *ha = vha->hw;
2c3dfe3f
SJ
32
33 /* Find an empty slot and assign an vp_id */
6c2f527c 34 mutex_lock(&ha->vport_lock);
eb66dc60
AV
35 vp_id = find_first_zero_bit(ha->vp_idx_map, ha->max_npiv_vports + 1);
36 if (vp_id > ha->max_npiv_vports) {
37 DEBUG15(printk ("vp_id %d is bigger than max-supported %d.\n",
38 vp_id, ha->max_npiv_vports));
6c2f527c 39 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
40 return vp_id;
41 }
42
eb66dc60 43 set_bit(vp_id, ha->vp_idx_map);
2c3dfe3f 44 ha->num_vhosts++;
711c1d91 45 ha->cur_vport_count++;
2c3dfe3f 46 vha->vp_idx = vp_id;
7b867cf7 47 list_add_tail(&vha->list, &ha->vp_list);
6c2f527c 48 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
49 return vp_id;
50}
51
52void
53qla24xx_deallocate_vp_id(scsi_qla_host_t *vha)
54{
55 uint16_t vp_id;
7b867cf7 56 struct qla_hw_data *ha = vha->hw;
2c3dfe3f 57
6c2f527c 58 mutex_lock(&ha->vport_lock);
2c3dfe3f
SJ
59 vp_id = vha->vp_idx;
60 ha->num_vhosts--;
711c1d91 61 ha->cur_vport_count--;
eb66dc60 62 clear_bit(vp_id, ha->vp_idx_map);
7b867cf7 63 list_del(&vha->list);
6c2f527c 64 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
65}
66
a824ebb3 67static scsi_qla_host_t *
7b867cf7 68qla24xx_find_vhost_by_name(struct qla_hw_data *ha, uint8_t *port_name)
2c3dfe3f
SJ
69{
70 scsi_qla_host_t *vha;
ee546b6e 71 struct scsi_qla_host *tvha;
2c3dfe3f
SJ
72
73 /* Locate matching device in database. */
ee546b6e 74 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
2c3dfe3f
SJ
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 */
26ff776d 94static void
2c3dfe3f
SJ
95qla2x00_mark_vp_devices_dead(scsi_qla_host_t *vha)
96{
97 fc_port_t *fcport;
2c3dfe3f 98
7b867cf7 99 list_for_each_entry(fcport, &vha->vp_fcports, list) {
2c3dfe3f
SJ
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
73208dfd 104 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2c3dfe3f 105 qla2x00_mark_device_lost(vha, fcport, 0, 0);
c9c5ced9 106 atomic_set(&fcport->state, FCS_UNCONFIGURED);
2c3dfe3f
SJ
107 }
108}
109
110int
111qla24xx_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
2c3dfe3f
SJ
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
131int
132qla24xx_enable_vp(scsi_qla_host_t *vha)
133{
134 int ret;
7b867cf7
AC
135 struct qla_hw_data *ha = vha->hw;
136 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
2c3dfe3f
SJ
137
138 /* Check if physical ha port is Up */
7b867cf7
AC
139 if (atomic_read(&base_vha->loop_state) == LOOP_DOWN ||
140 atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
2c3dfe3f
SJ
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 */
6c2f527c 147 mutex_lock(&ha->vport_lock);
2c3dfe3f 148 ret = qla24xx_modify_vp_config(vha);
6c2f527c 149 mutex_unlock(&ha->vport_lock);
2c3dfe3f
SJ
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
160enable_failed:
161 DEBUG15(qla_printk(KERN_INFO, ha,
162 "Virtual port with id: %d - Disabled\n", vha->vp_idx));
163 return 1;
164}
165
26ff776d 166static void
2c3dfe3f
SJ
167qla24xx_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) {
7b867cf7
AC
178 DEBUG15(qla_printk(KERN_ERR, vha->hw, "Failed to enable "
179 "receiving of RSCN requests: 0x%x\n", ret));
2c3dfe3f
SJ
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
194void
73208dfd 195qla2x00_alert_all_vps(struct rsp_que *rsp, uint16_t *mb)
2c3dfe3f 196{
ee546b6e 197 scsi_qla_host_t *vha, *tvha;
73208dfd 198 struct qla_hw_data *ha = rsp->hw;
7b867cf7 199 int i = 0;
2c3dfe3f 200
ee546b6e 201 list_for_each_entry_safe(vha, tvha, &ha->vp_list, list) {
7b867cf7 202 if (vha->vp_idx) {
2c3dfe3f
SJ
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"
7b867cf7
AC
213 " VP[%d], mb = 0x%x, vha=%p\n",
214 vha->host_no, __func__, i, *mb, vha));
73208dfd 215 qla2x00_async_event(vha, rsp, mb);
2c3dfe3f
SJ
216 break;
217 }
218 }
7b867cf7 219 i++;
2c3dfe3f
SJ
220 }
221}
222
7b867cf7 223int
2c3dfe3f
SJ
224qla2x00_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
7b867cf7
AC
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
2c3dfe3f
SJ
242 DEBUG15(printk("scsi(%ld): Scheduling enable of Vport %d...\n",
243 vha->host_no, vha->vp_idx));
7b867cf7 244 return qla24xx_enable_vp(vha);
2c3dfe3f
SJ
245}
246
a824ebb3 247static int
2c3dfe3f
SJ
248qla2x00_do_dpc_vp(scsi_qla_host_t *vha)
249{
7b867cf7
AC
250 struct qla_hw_data *ha = vha->hw;
251 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
221726d4 252
2c3dfe3f
SJ
253 if (test_and_clear_bit(VP_IDX_ACQUIRED, &vha->vp_flags)) {
254 /* VP acquired. complete port configuration */
7b867cf7 255 if (atomic_read(&base_vha->loop_state) == LOOP_READY) {
221726d4
SJ
256 qla24xx_configure_vp(vha);
257 } else {
258 set_bit(VP_IDX_ACQUIRED, &vha->vp_flags);
7b867cf7 259 set_bit(VP_DPC_NEEDED, &base_vha->dpc_flags);
221726d4
SJ
260 }
261
2c3dfe3f
SJ
262 return 0;
263 }
264
7b867cf7
AC
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 }
2c3dfe3f
SJ
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
73208dfd 287 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
2c3dfe3f
SJ
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
297void
7b867cf7 298qla2x00_do_dpc_all_vps(scsi_qla_host_t *vha)
2c3dfe3f
SJ
299{
300 int ret;
7b867cf7
AC
301 struct qla_hw_data *ha = vha->hw;
302 scsi_qla_host_t *vp;
ee546b6e 303 struct scsi_qla_host *tvp;
2c3dfe3f 304
7b867cf7 305 if (vha->vp_idx)
2c3dfe3f
SJ
306 return;
307 if (list_empty(&ha->vp_list))
308 return;
309
7b867cf7 310 clear_bit(VP_DPC_NEEDED, &vha->dpc_flags);
2c3dfe3f 311
ee546b6e 312 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7b867cf7
AC
313 if (vp->vp_idx)
314 ret = qla2x00_do_dpc_vp(vp);
2c3dfe3f
SJ
315 }
316}
317
318int
319qla24xx_vport_create_req_sanity_check(struct fc_vport *fc_vport)
320{
7b867cf7
AC
321 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
322 struct qla_hw_data *ha = base_vha->hw;
2c3dfe3f
SJ
323 scsi_qla_host_t *vha;
324 uint8_t port_name[WWN_SIZE];
325
326 if (fc_vport->roles != FC_PORT_ROLE_FCP_INITIATOR)
327 return VPCERR_UNSUPPORTED;
328
329 /* Check up the F/W and H/W support NPIV */
330 if (!ha->flags.npiv_supported)
331 return VPCERR_UNSUPPORTED;
332
333 /* Check up whether npiv supported switch presented */
334 if (!(ha->switch_cap & FLOGI_MID_SUPPORT))
335 return VPCERR_NO_FABRIC_SUPP;
336
337 /* Check up unique WWPN */
338 u64_to_wwn(fc_vport->port_name, port_name);
7b867cf7 339 if (!memcmp(port_name, base_vha->port_name, WWN_SIZE))
50db6b13 340 return VPCERR_BAD_WWN;
2c3dfe3f
SJ
341 vha = qla24xx_find_vhost_by_name(ha, port_name);
342 if (vha)
343 return VPCERR_BAD_WWN;
344
345 /* Check up max-npiv-supports */
346 if (ha->num_vhosts > ha->max_npiv_vports) {
eb66dc60 347 DEBUG15(printk("scsi(%ld): num_vhosts %ud is bigger than "
7b867cf7 348 "max_npv_vports %ud.\n", base_vha->host_no,
eb66dc60 349 ha->num_vhosts, ha->max_npiv_vports));
2c3dfe3f
SJ
350 return VPCERR_UNSUPPORTED;
351 }
352 return 0;
353}
354
355scsi_qla_host_t *
356qla24xx_create_vhost(struct fc_vport *fc_vport)
357{
7b867cf7
AC
358 scsi_qla_host_t *base_vha = shost_priv(fc_vport->shost);
359 struct qla_hw_data *ha = base_vha->hw;
2c3dfe3f 360 scsi_qla_host_t *vha;
a5326f86 361 struct scsi_host_template *sht = &qla2xxx_driver_template;
2c3dfe3f
SJ
362 struct Scsi_Host *host;
363
7b867cf7
AC
364 vha = qla2x00_create_host(sht, ha);
365 if (!vha) {
366 DEBUG(printk("qla2xxx: scsi_host_alloc() failed for vport\n"));
2c3dfe3f
SJ
367 return(NULL);
368 }
369
7b867cf7 370 host = vha->host;
2c3dfe3f 371 fc_vport->dd_data = vha;
2c3dfe3f
SJ
372 /* New host info */
373 u64_to_wwn(fc_vport->node_name, vha->node_name);
374 u64_to_wwn(fc_vport->port_name, vha->port_name);
375
2c3dfe3f
SJ
376 vha->fc_vport = fc_vport;
377 vha->device_flags = 0;
2c3dfe3f
SJ
378 vha->vp_idx = qla24xx_allocate_vp_id(vha);
379 if (vha->vp_idx > ha->max_npiv_vports) {
380 DEBUG15(printk("scsi(%ld): Couldn't allocate vp_id.\n",
381 vha->host_no));
7b867cf7 382 goto create_vhost_failed;
2c3dfe3f
SJ
383 }
384 vha->mgmt_svr_loop_id = 10 + vha->vp_idx;
385
2c3dfe3f
SJ
386 vha->dpc_flags = 0L;
387 set_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags);
388 set_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags);
389
390 /*
391 * To fix the issue of processing a parent's RSCN for the vport before
392 * its SCR is complete.
393 */
394 set_bit(VP_SCR_NEEDED, &vha->vp_flags);
395 atomic_set(&vha->loop_state, LOOP_DOWN);
396 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
397
398 qla2x00_start_timer(vha, qla2x00_timer, WATCH_INTERVAL);
399
2afa19a9
AC
400 vha->req = base_vha->req;
401 host->can_queue = base_vha->req->length + 128;
2c3dfe3f
SJ
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;
711c1d91 407 host->unique_id = host->host_no;
2c3dfe3f
SJ
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;
2c3dfe3f
SJ
415
416 return vha;
417
7b867cf7 418create_vhost_failed:
2c3dfe3f
SJ
419 return NULL;
420}
73208dfd
AC
421
422static void
423qla25xx_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
442static void
443qla25xx_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
467int
468qla25xx_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;
618a7523 474 ret = qla25xx_init_req_que(vha, req);
73208dfd
AC
475 }
476 if (ret == QLA_SUCCESS)
477 qla25xx_free_req_que(vha, req);
478
479 return ret;
480}
481
482int
483qla25xx_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;
618a7523 489 ret = qla25xx_init_rsp_que(vha, rsp);
73208dfd
AC
490 }
491 if (ret == QLA_SUCCESS)
492 qla25xx_free_rsp_que(vha, rsp);
493
494 return ret;
495}
496
497int 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;
618a7523 505 ret = qla25xx_init_req_que(vha, req);
73208dfd
AC
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 */
515int
2afa19a9 516qla25xx_delete_queues(struct scsi_qla_host *vha)
73208dfd
AC
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
2afa19a9
AC
523 /* Delete request queues */
524 for (cnt = 1; cnt < ha->max_req_queues; cnt++) {
525 req = ha->req_q_map[cnt];
73208dfd 526 if (req) {
73208dfd
AC
527 ret = qla25xx_delete_req_que(vha, req);
528 if (ret != QLA_SUCCESS) {
529 qla_printk(KERN_WARNING, ha,
2afa19a9
AC
530 "Couldn't delete req que %d\n",
531 req->id);
73208dfd
AC
532 return ret;
533 }
73208dfd 534 }
2afa19a9
AC
535 }
536
537 /* Delete response queues */
538 for (cnt = 1; cnt < ha->max_rsp_queues; cnt++) {
539 rsp = ha->rsp_q_map[cnt];
540 if (rsp) {
541 ret = qla25xx_delete_rsp_que(vha, rsp);
542 if (ret != QLA_SUCCESS) {
543 qla_printk(KERN_WARNING, ha,
544 "Couldn't delete rsp que %d\n",
545 rsp->id);
546 return ret;
73208dfd
AC
547 }
548 }
549 }
73208dfd
AC
550 return ret;
551}
552
553int
554qla25xx_create_req_que(struct qla_hw_data *ha, uint16_t options,
2afa19a9 555 uint8_t vp_idx, uint16_t rid, int rsp_que, uint8_t qos)
73208dfd
AC
556{
557 int ret = 0;
558 struct req_que *req = NULL;
559 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
560 uint16_t que_id = 0;
08029990 561 device_reg_t __iomem *reg;
2afa19a9 562 uint32_t cnt;
73208dfd
AC
563
564 req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
565 if (req == NULL) {
566 qla_printk(KERN_WARNING, ha, "could not allocate memory"
567 "for request que\n");
568 goto que_failed;
569 }
570
571 req->length = REQUEST_ENTRY_CNT_24XX;
572 req->ring = dma_alloc_coherent(&ha->pdev->dev,
573 (req->length + 1) * sizeof(request_t),
574 &req->dma, GFP_KERNEL);
575 if (req->ring == NULL) {
576 qla_printk(KERN_WARNING, ha,
577 "Memory Allocation failed - request_ring\n");
578 goto que_failed;
579 }
580
581 mutex_lock(&ha->vport_lock);
2afa19a9
AC
582 que_id = find_first_zero_bit(ha->req_qid_map, ha->max_req_queues);
583 if (que_id >= ha->max_req_queues) {
73208dfd
AC
584 mutex_unlock(&ha->vport_lock);
585 qla_printk(KERN_INFO, ha, "No resources to create "
586 "additional request queue\n");
587 goto que_failed;
588 }
589 set_bit(que_id, ha->req_qid_map);
590 ha->req_q_map[que_id] = req;
591 req->rid = rid;
592 req->vp_idx = vp_idx;
593 req->qos = qos;
594
2afa19a9
AC
595 if (rsp_que < 0)
596 req->rsp = NULL;
597 else
73208dfd
AC
598 req->rsp = ha->rsp_q_map[rsp_que];
599 /* Use alternate PCI bus number */
600 if (MSB(req->rid))
601 options |= BIT_4;
602 /* Use alternate PCI devfn */
603 if (LSB(req->rid))
604 options |= BIT_5;
605 req->options = options;
2afa19a9
AC
606
607 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++)
608 req->outstanding_cmds[cnt] = NULL;
609 req->current_outstanding_cmd = 1;
610
73208dfd
AC
611 req->ring_ptr = req->ring;
612 req->ring_index = 0;
613 req->cnt = req->length;
614 req->id = que_id;
08029990 615 reg = ISP_QUE_REG(ha, que_id);
29bdccbe 616 req->max_q_depth = ha->req_q_map[0]->max_q_depth;
73208dfd
AC
617 mutex_unlock(&ha->vport_lock);
618
618a7523 619 ret = qla25xx_init_req_que(base_vha, req);
73208dfd
AC
620 if (ret != QLA_SUCCESS) {
621 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
622 mutex_lock(&ha->vport_lock);
623 clear_bit(que_id, ha->req_qid_map);
624 mutex_unlock(&ha->vport_lock);
625 goto que_failed;
626 }
627
628 return req->id;
629
630que_failed:
631 qla25xx_free_req_que(base_vha, req);
632 return 0;
633}
634
68ca949c
AC
635static void qla_do_work(struct work_struct *work)
636{
637 struct rsp_que *rsp = container_of(work, struct rsp_que, q_work);
638 struct scsi_qla_host *vha;
639
640 vha = qla25xx_get_host(rsp);
641 qla24xx_process_response_queue(vha, rsp);
642}
643
73208dfd
AC
644/* create response queue */
645int
646qla25xx_create_rsp_que(struct qla_hw_data *ha, uint16_t options,
2afa19a9 647 uint8_t vp_idx, uint16_t rid, int req)
73208dfd
AC
648{
649 int ret = 0;
650 struct rsp_que *rsp = NULL;
651 struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
08029990
AV
652 uint16_t que_id = 0;
653 device_reg_t __iomem *reg;
73208dfd
AC
654
655 rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
656 if (rsp == NULL) {
657 qla_printk(KERN_WARNING, ha, "could not allocate memory for"
658 " response que\n");
659 goto que_failed;
660 }
661
2afa19a9 662 rsp->length = RESPONSE_ENTRY_CNT_MQ;
73208dfd
AC
663 rsp->ring = dma_alloc_coherent(&ha->pdev->dev,
664 (rsp->length + 1) * sizeof(response_t),
665 &rsp->dma, GFP_KERNEL);
666 if (rsp->ring == NULL) {
667 qla_printk(KERN_WARNING, ha,
668 "Memory Allocation failed - response_ring\n");
669 goto que_failed;
670 }
671
672 mutex_lock(&ha->vport_lock);
2afa19a9
AC
673 que_id = find_first_zero_bit(ha->rsp_qid_map, ha->max_rsp_queues);
674 if (que_id >= ha->max_rsp_queues) {
73208dfd
AC
675 mutex_unlock(&ha->vport_lock);
676 qla_printk(KERN_INFO, ha, "No resources to create "
677 "additional response queue\n");
678 goto que_failed;
679 }
680 set_bit(que_id, ha->rsp_qid_map);
681
682 if (ha->flags.msix_enabled)
683 rsp->msix = &ha->msix_entries[que_id + 1];
684 else
685 qla_printk(KERN_WARNING, ha, "msix not enabled\n");
686
687 ha->rsp_q_map[que_id] = rsp;
688 rsp->rid = rid;
689 rsp->vp_idx = vp_idx;
690 rsp->hw = ha;
691 /* Use alternate PCI bus number */
692 if (MSB(rsp->rid))
693 options |= BIT_4;
694 /* Use alternate PCI devfn */
695 if (LSB(rsp->rid))
696 options |= BIT_5;
697 rsp->options = options;
73208dfd 698 rsp->id = que_id;
08029990
AV
699 reg = ISP_QUE_REG(ha, que_id);
700 rsp->rsp_q_in = &reg->isp25mq.rsp_q_in;
701 rsp->rsp_q_out = &reg->isp25mq.rsp_q_out;
73208dfd
AC
702 mutex_unlock(&ha->vport_lock);
703
704 ret = qla25xx_request_irq(rsp);
705 if (ret)
706 goto que_failed;
707
618a7523 708 ret = qla25xx_init_rsp_que(base_vha, rsp);
73208dfd
AC
709 if (ret != QLA_SUCCESS) {
710 qla_printk(KERN_WARNING, ha, "%s failed\n", __func__);
711 mutex_lock(&ha->vport_lock);
712 clear_bit(que_id, ha->rsp_qid_map);
713 mutex_unlock(&ha->vport_lock);
714 goto que_failed;
715 }
2afa19a9
AC
716 if (req >= 0)
717 rsp->req = ha->req_q_map[req];
718 else
719 rsp->req = NULL;
73208dfd
AC
720
721 qla2x00_init_response_q_entries(rsp);
68ca949c
AC
722 if (rsp->hw->wq)
723 INIT_WORK(&rsp->q_work, qla_do_work);
73208dfd
AC
724 return rsp->id;
725
726que_failed:
727 qla25xx_free_rsp_que(base_vha, rsp);
728 return 0;
729}
730
731int
732qla25xx_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;
2afa19a9 737 struct rsp_que *rsp;
73208dfd
AC
738
739 options |= BIT_1;
2afa19a9 740 ret = qla25xx_create_rsp_que(ha, options, vha->vp_idx, 0, -1);
73208dfd
AC
741 if (!ret) {
742 qla_printk(KERN_WARNING, ha, "Response Que create failed\n");
743 return ret;
744 } else
745 qla_printk(KERN_INFO, ha, "Response Que:%d created.\n", ret);
2afa19a9 746 rsp = ha->rsp_q_map[ret];
73208dfd
AC
747
748 options = 0;
749 if (qos & BIT_7)
750 options |= BIT_8;
751 ret = qla25xx_create_req_que(ha, options, vha->vp_idx, 0, ret,
752 qos & ~BIT_7);
753 if (ret) {
2afa19a9 754 vha->req = ha->req_q_map[ret];
73208dfd
AC
755 qla_printk(KERN_INFO, ha, "Request Que:%d created.\n", ret);
756 } else
757 qla_printk(KERN_WARNING, ha, "Request Que create failed\n");
2afa19a9 758 rsp->req = ha->req_q_map[ret];
73208dfd
AC
759
760 return ret;
761}