[SCSI] qla2xxx: Re-organized BSG interface specific code.
[linux-2.6-block.git] / drivers / scsi / qla2xxx / qla_bsg.c
CommitLineData
6e98016c
GM
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
9#include <linux/kthread.h>
10#include <linux/vmalloc.h>
11#include <linux/delay.h>
12
13/* BSG support for ELS/CT pass through */
14inline srb_t *
15qla2x00_get_ctx_bsg_sp(scsi_qla_host_t *vha, fc_port_t *fcport, size_t size)
16{
17 srb_t *sp;
18 struct qla_hw_data *ha = vha->hw;
19 struct srb_bsg_ctx *ctx;
20
21 sp = mempool_alloc(ha->srb_mempool, GFP_KERNEL);
22 if (!sp)
23 goto done;
24 ctx = kzalloc(size, GFP_KERNEL);
25 if (!ctx) {
26 mempool_free(sp, ha->srb_mempool);
27 sp = NULL;
28 goto done;
29 }
30
31 memset(sp, 0, sizeof(*sp));
32 sp->fcport = fcport;
33 sp->ctx = ctx;
34done:
35 return sp;
36}
37
38static int
39qla2x00_process_els(struct fc_bsg_job *bsg_job)
40{
41 struct fc_rport *rport;
42 fc_port_t *fcport;
43 struct Scsi_Host *host;
44 scsi_qla_host_t *vha;
45 struct qla_hw_data *ha;
46 srb_t *sp;
47 const char *type;
48 int req_sg_cnt, rsp_sg_cnt;
49 int rval = (DRIVER_ERROR << 16);
50 uint16_t nextlid = 0;
51 struct srb_bsg *els;
52
53 /* Multiple SG's are not supported for ELS requests */
54 if (bsg_job->request_payload.sg_cnt > 1 ||
55 bsg_job->reply_payload.sg_cnt > 1) {
56 DEBUG2(printk(KERN_INFO
57 "multiple SG's are not supported for ELS requests"
58 " [request_sg_cnt: %x reply_sg_cnt: %x]\n",
59 bsg_job->request_payload.sg_cnt,
60 bsg_job->reply_payload.sg_cnt));
61 rval = -EPERM;
62 goto done;
63 }
64
65 /* ELS request for rport */
66 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
67 rport = bsg_job->rport;
68 fcport = *(fc_port_t **) rport->dd_data;
69 host = rport_to_shost(rport);
70 vha = shost_priv(host);
71 ha = vha->hw;
72 type = "FC_BSG_RPT_ELS";
73
74 /* make sure the rport is logged in,
75 * if not perform fabric login
76 */
77 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
78 DEBUG2(qla_printk(KERN_WARNING, ha,
79 "failed to login port %06X for ELS passthru\n",
80 fcport->d_id.b24));
81 rval = -EIO;
82 goto done;
83 }
84 } else {
85 host = bsg_job->shost;
86 vha = shost_priv(host);
87 ha = vha->hw;
88 type = "FC_BSG_HST_ELS_NOLOGIN";
89
90 /* Allocate a dummy fcport structure, since functions
91 * preparing the IOCB and mailbox command retrieves port
92 * specific information from fcport structure. For Host based
93 * ELS commands there will be no fcport structure allocated
94 */
95 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
96 if (!fcport) {
97 rval = -ENOMEM;
98 goto done;
99 }
100
101 /* Initialize all required fields of fcport */
102 fcport->vha = vha;
103 fcport->vp_idx = vha->vp_idx;
104 fcport->d_id.b.al_pa =
105 bsg_job->request->rqst_data.h_els.port_id[0];
106 fcport->d_id.b.area =
107 bsg_job->request->rqst_data.h_els.port_id[1];
108 fcport->d_id.b.domain =
109 bsg_job->request->rqst_data.h_els.port_id[2];
110 fcport->loop_id =
111 (fcport->d_id.b.al_pa == 0xFD) ?
112 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
113 }
114
115 if (!vha->flags.online) {
116 DEBUG2(qla_printk(KERN_WARNING, ha,
117 "host not online\n"));
118 rval = -EIO;
119 goto done;
120 }
121
122 req_sg_cnt =
123 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
124 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
125 if (!req_sg_cnt) {
126 rval = -ENOMEM;
127 goto done_free_fcport;
128 }
129 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
130 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
131 if (!rsp_sg_cnt) {
132 rval = -ENOMEM;
133 goto done_free_fcport;
134 }
135
136 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
137 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt))
138 {
139 DEBUG2(printk(KERN_INFO
140 "dma mapping resulted in different sg counts \
141 [request_sg_cnt: %x dma_request_sg_cnt: %x\
142 reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
143 bsg_job->request_payload.sg_cnt, req_sg_cnt,
144 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
145 rval = -EAGAIN;
146 goto done_unmap_sg;
147 }
148
149 /* Alloc SRB structure */
150 sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_bsg));
151 if (!sp) {
152 rval = -ENOMEM;
153 goto done_unmap_sg;
154 }
155
156 els = sp->ctx;
157 els->ctx.type =
158 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
159 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
160 els->bsg_job = bsg_job;
161
162 DEBUG2(qla_printk(KERN_INFO, ha,
163 "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
164 "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
165 bsg_job->request->rqst_data.h_els.command_code,
166 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
167 fcport->d_id.b.al_pa));
168
169 rval = qla2x00_start_sp(sp);
170 if (rval != QLA_SUCCESS) {
171 kfree(sp->ctx);
172 mempool_free(sp, ha->srb_mempool);
173 rval = -EIO;
174 goto done_unmap_sg;
175 }
176 return rval;
177
178done_unmap_sg:
179 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
180 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
181 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
182 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
183 goto done_free_fcport;
184
185done_free_fcport:
186 if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
187 kfree(fcport);
188done:
189 return rval;
190}
191
192static int
193qla2x00_process_ct(struct fc_bsg_job *bsg_job)
194{
195 srb_t *sp;
196 struct Scsi_Host *host = bsg_job->shost;
197 scsi_qla_host_t *vha = shost_priv(host);
198 struct qla_hw_data *ha = vha->hw;
199 int rval = (DRIVER_ERROR << 16);
200 int req_sg_cnt, rsp_sg_cnt;
201 uint16_t loop_id;
202 struct fc_port *fcport;
203 char *type = "FC_BSG_HST_CT";
204 struct srb_bsg *ct;
205
206 /* pass through is supported only for ISP 4Gb or higher */
207 if (!IS_FWI2_CAPABLE(ha)) {
208 DEBUG2(qla_printk(KERN_INFO, ha,
209 "scsi(%ld):Firmware is not capable to support FC "
210 "CT pass thru\n", vha->host_no));
211 rval = -EPERM;
212 goto done;
213 }
214
215 req_sg_cnt =
216 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
217 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
218 if (!req_sg_cnt) {
219 rval = -ENOMEM;
220 goto done;
221 }
222
223 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
224 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
225 if (!rsp_sg_cnt) {
226 rval = -ENOMEM;
227 goto done;
228 }
229
230 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
231 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt))
232 {
233 DEBUG2(qla_printk(KERN_WARNING, ha,
234 "[request_sg_cnt: %x dma_request_sg_cnt: %x\
235 reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
236 bsg_job->request_payload.sg_cnt, req_sg_cnt,
237 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt));
238 rval = -EAGAIN;
239 goto done_unmap_sg;
240 }
241
242 if (!vha->flags.online) {
243 DEBUG2(qla_printk(KERN_WARNING, ha,
244 "host not online\n"));
245 rval = -EIO;
246 goto done_unmap_sg;
247 }
248
249 loop_id =
250 (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
251 >> 24;
252 switch (loop_id) {
253 case 0xFC:
254 loop_id = cpu_to_le16(NPH_SNS);
255 break;
256 case 0xFA:
257 loop_id = vha->mgmt_svr_loop_id;
258 break;
259 default:
260 DEBUG2(qla_printk(KERN_INFO, ha,
261 "Unknown loop id: %x\n", loop_id));
262 rval = -EINVAL;
263 goto done_unmap_sg;
264 }
265
266 /* Allocate a dummy fcport structure, since functions preparing the
267 * IOCB and mailbox command retrieves port specific information
268 * from fcport structure. For Host based ELS commands there will be
269 * no fcport structure allocated
270 */
271 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
272 if (!fcport)
273 {
274 rval = -ENOMEM;
275 goto done_unmap_sg;
276 }
277
278 /* Initialize all required fields of fcport */
279 fcport->vha = vha;
280 fcport->vp_idx = vha->vp_idx;
281 fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
282 fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
283 fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
284 fcport->loop_id = loop_id;
285
286 /* Alloc SRB structure */
287 sp = qla2x00_get_ctx_bsg_sp(vha, fcport, sizeof(struct srb_bsg));
288 if (!sp) {
289 rval = -ENOMEM;
290 goto done_free_fcport;
291 }
292
293 ct = sp->ctx;
294 ct->ctx.type = SRB_CT_CMD;
295 ct->bsg_job = bsg_job;
296
297 DEBUG2(qla_printk(KERN_INFO, ha,
298 "scsi(%ld:%x): bsg rqst type: %s els type: %x - loop-id=%x "
299 "portid=%02x%02x%02x.\n", vha->host_no, sp->handle, type,
300 (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
301 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
302 fcport->d_id.b.al_pa));
303
304 rval = qla2x00_start_sp(sp);
305 if (rval != QLA_SUCCESS) {
306 kfree(sp->ctx);
307 mempool_free(sp, ha->srb_mempool);
308 rval = -EIO;
309 goto done_free_fcport;
310 }
311 return rval;
312
313done_free_fcport:
314 kfree(fcport);
315done_unmap_sg:
316 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
317 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
318 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
319 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
320done:
321 return rval;
322}
323
324static int
325qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
326{
327 struct Scsi_Host *host = bsg_job->shost;
328 scsi_qla_host_t *vha = shost_priv(host);
329 struct qla_hw_data *ha = vha->hw;
330 int rval;
331 uint8_t command_sent;
332 char *type;
333 struct msg_echo_lb elreq;
334 uint16_t response[MAILBOX_REGISTER_COUNT];
335 uint8_t* fw_sts_ptr;
336 uint8_t *req_data = NULL;
337 dma_addr_t req_data_dma;
338 uint32_t req_data_len;
339 uint8_t *rsp_data = NULL;
340 dma_addr_t rsp_data_dma;
341 uint32_t rsp_data_len;
342
343 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
344 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
345 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
346 return -EBUSY;
347
348 if (!vha->flags.online) {
349 DEBUG2(qla_printk(KERN_WARNING, ha, "host not online\n"));
350 return -EIO;
351 }
352
353 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
354 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
355 DMA_TO_DEVICE);
356
357 if (!elreq.req_sg_cnt)
358 return -ENOMEM;
359
360 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
361 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
362 DMA_FROM_DEVICE);
363
364 if (!elreq.rsp_sg_cnt) {
365 rval = -ENOMEM;
366 goto done_unmap_req_sg;
367}
368
369 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
370 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
371 DEBUG2(printk(KERN_INFO
372 "dma mapping resulted in different sg counts "
373 "[request_sg_cnt: %x dma_request_sg_cnt: %x "
374 "reply_sg_cnt: %x dma_reply_sg_cnt: %x]\n",
375 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
376 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt));
377 rval = -EAGAIN;
378 goto done_unmap_sg;
379 }
380 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
381 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
382 &req_data_dma, GFP_KERNEL);
383 if (!req_data) {
384 DEBUG2(printk(KERN_ERR "%s: dma alloc for req_data "
385 "failed for host=%lu\n", __func__, vha->host_no));
386 rval = -ENOMEM;
387 goto done_unmap_sg;
388 }
389
390 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
391 &rsp_data_dma, GFP_KERNEL);
392 if (!rsp_data) {
393 DEBUG2(printk(KERN_ERR "%s: dma alloc for rsp_data "
394 "failed for host=%lu\n", __func__, vha->host_no));
395 rval = -ENOMEM;
396 goto done_free_dma_req;
397 }
398
399 /* Copy the request buffer in req_data now */
400 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
401 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
402
403 elreq.send_dma = req_data_dma;
404 elreq.rcv_dma = rsp_data_dma;
405 elreq.transfer_size = req_data_len;
406
407 elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
408
409 if (ha->current_topology != ISP_CFG_F) {
410 type = "FC_BSG_HST_VENDOR_LOOPBACK";
411 DEBUG2(qla_printk(KERN_INFO, ha,
412 "scsi(%ld) bsg rqst type: %s\n",
413 vha->host_no, type));
414
415 command_sent = INT_DEF_LB_LOOPBACK_CMD;
416 rval = qla2x00_loopback_test(vha, &elreq, response);
417 if (IS_QLA81XX(ha)) {
418 if (response[0] == MBS_COMMAND_ERROR &&
419 response[1] == MBS_LB_RESET) {
420 DEBUG2(printk(KERN_ERR "%s(%ld): ABORTing "
421 "ISP\n", __func__, vha->host_no));
422 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
423 qla2xxx_wake_dpc(vha);
424 }
425 }
426 } else {
427 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
428 DEBUG2(qla_printk(KERN_INFO, ha,
429 "scsi(%ld) bsg rqst type: %s\n" ,vha->host_no, type));
430 command_sent = INT_DEF_LB_ECHO_CMD;
431 rval = qla2x00_echo_test(vha, &elreq, response);
432 }
433
434 if (rval) {
435 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
436 "request %s failed\n", vha->host_no, type));
437
438 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
439 sizeof(struct fc_bsg_reply);
440
441 memcpy(fw_sts_ptr, response, sizeof(response));
442 fw_sts_ptr += sizeof(response);
443 *fw_sts_ptr = command_sent;
444 rval = 0;
445 bsg_job->reply->reply_payload_rcv_len = 0;
446 bsg_job->reply->result = (DID_ERROR << 16);
447 } else {
448 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
449 "request %s completed\n", vha->host_no, type));
450
451 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
452 sizeof(response) + sizeof(uint8_t);
453 bsg_job->reply->reply_payload_rcv_len =
454 bsg_job->reply_payload.payload_len;
455 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
456 sizeof(struct fc_bsg_reply);
457 memcpy(fw_sts_ptr, response, sizeof(response));
458 fw_sts_ptr += sizeof(response);
459 *fw_sts_ptr = command_sent;
460 bsg_job->reply->result = DID_OK;
461 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
462 bsg_job->reply_payload.sg_cnt, rsp_data,
463 rsp_data_len);
464 }
465 bsg_job->job_done(bsg_job);
466
467 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
468 rsp_data, rsp_data_dma);
469done_free_dma_req:
470 dma_free_coherent(&ha->pdev->dev, req_data_len,
471 req_data, req_data_dma);
472done_unmap_sg:
473 dma_unmap_sg(&ha->pdev->dev,
474 bsg_job->reply_payload.sg_list,
475 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
476done_unmap_req_sg:
477 dma_unmap_sg(&ha->pdev->dev,
478 bsg_job->request_payload.sg_list,
479 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
480 return rval;
481}
482
483static int
484qla84xx_reset(struct fc_bsg_job *bsg_job)
485{
486 struct Scsi_Host *host = bsg_job->shost;
487 scsi_qla_host_t *vha = shost_priv(host);
488 struct qla_hw_data *ha = vha->hw;
489 int rval = 0;
490 uint32_t flag;
491
492 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
493 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
494 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
495 return -EBUSY;
496
497 if (!IS_QLA84XX(ha)) {
498 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
499 "exiting.\n", vha->host_no));
500 return -EINVAL;
501 }
502
503 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
504
505 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
506
507 if (rval) {
508 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
509 "request 84xx reset failed\n", vha->host_no));
510 rval = bsg_job->reply->reply_payload_rcv_len = 0;
511 bsg_job->reply->result = (DID_ERROR << 16);
512
513 } else {
514 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
515 "request 84xx reset completed\n", vha->host_no));
516 bsg_job->reply->result = DID_OK;
517 }
518
519 bsg_job->job_done(bsg_job);
520 return rval;
521}
522
523static int
524qla84xx_updatefw(struct fc_bsg_job *bsg_job)
525{
526 struct Scsi_Host *host = bsg_job->shost;
527 scsi_qla_host_t *vha = shost_priv(host);
528 struct qla_hw_data *ha = vha->hw;
529 struct verify_chip_entry_84xx *mn = NULL;
530 dma_addr_t mn_dma, fw_dma;
531 void *fw_buf = NULL;
532 int rval = 0;
533 uint32_t sg_cnt;
534 uint32_t data_len;
535 uint16_t options;
536 uint32_t flag;
537 uint32_t fw_ver;
538
539 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
540 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
541 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
542 return -EBUSY;
543
544 if (!IS_QLA84XX(ha)) {
545 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
546 "exiting.\n", vha->host_no));
547 return -EINVAL;
548 }
549
550 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
551 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
552 if (!sg_cnt)
553 return -ENOMEM;
554
555 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
556 DEBUG2(printk(KERN_INFO
557 "dma mapping resulted in different sg counts "
558 "request_sg_cnt: %x dma_request_sg_cnt: %x ",
559 bsg_job->request_payload.sg_cnt, sg_cnt));
560 rval = -EAGAIN;
561 goto done_unmap_sg;
562 }
563
564 data_len = bsg_job->request_payload.payload_len;
565 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
566 &fw_dma, GFP_KERNEL);
567 if (!fw_buf) {
568 DEBUG2(printk(KERN_ERR "%s: dma alloc for fw_buf "
569 "failed for host=%lu\n", __func__, vha->host_no));
570 rval = -ENOMEM;
571 goto done_unmap_sg;
572 }
573
574 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
575 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
576
577 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
578 if (!mn) {
579 DEBUG2(printk(KERN_ERR "%s: dma alloc for fw buffer "
580 "failed for host=%lu\n", __func__, vha->host_no));
581 rval = -ENOMEM;
582 goto done_free_fw_buf;
583 }
584
585 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
586 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
587
588 memset(mn, 0, sizeof(struct access_chip_84xx));
589 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
590 mn->entry_count = 1;
591
592 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
593 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
594 options |= VCO_DIAG_FW;
595
596 mn->options = cpu_to_le16(options);
597 mn->fw_ver = cpu_to_le32(fw_ver);
598 mn->fw_size = cpu_to_le32(data_len);
599 mn->fw_seq_size = cpu_to_le32(data_len);
600 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
601 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
602 mn->dseg_length = cpu_to_le32(data_len);
603 mn->data_seg_cnt = cpu_to_le16(1);
604
605 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
606
607 if (rval) {
608 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
609 "request 84xx updatefw failed\n", vha->host_no));
610
611 rval = bsg_job->reply->reply_payload_rcv_len = 0;
612 bsg_job->reply->result = (DID_ERROR << 16);
613
614 } else {
615 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
616 "request 84xx updatefw completed\n", vha->host_no));
617
618 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
619 bsg_job->reply->result = DID_OK;
620 }
621
622 bsg_job->job_done(bsg_job);
623 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
624
625done_free_fw_buf:
626 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
627
628done_unmap_sg:
629 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
630 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
631
632 return rval;
633}
634
635static int
636qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
637{
638 struct Scsi_Host *host = bsg_job->shost;
639 scsi_qla_host_t *vha = shost_priv(host);
640 struct qla_hw_data *ha = vha->hw;
641 struct access_chip_84xx *mn = NULL;
642 dma_addr_t mn_dma, mgmt_dma;
643 void *mgmt_b = NULL;
644 int rval = 0;
645 struct qla_bsg_a84_mgmt *ql84_mgmt;
646 uint32_t sg_cnt;
647 uint32_t data_len;
648 uint32_t dma_direction = DMA_NONE;
649
650 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
651 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
652 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
653 return -EBUSY;
654
655 if (!IS_QLA84XX(ha)) {
656 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld): Not 84xx, "
657 "exiting.\n", vha->host_no));
658 return -EINVAL;
659 }
660
661 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
662 sizeof(struct fc_bsg_request));
663 if (!ql84_mgmt) {
664 DEBUG2(printk("%s(%ld): mgmt header not provided, exiting.\n",
665 __func__, vha->host_no));
666 return -EINVAL;
667 }
668
669 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
670 if (!mn) {
671 DEBUG2(printk(KERN_ERR "%s: dma alloc for fw buffer "
672 "failed for host=%lu\n", __func__, vha->host_no));
673 return -ENOMEM;
674 }
675
676 memset(mn, 0, sizeof(struct access_chip_84xx));
677 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
678 mn->entry_count = 1;
679
680 switch (ql84_mgmt->mgmt.cmd) {
681 case QLA84_MGMT_READ_MEM:
682 case QLA84_MGMT_GET_INFO:
683 sg_cnt = dma_map_sg(&ha->pdev->dev,
684 bsg_job->reply_payload.sg_list,
685 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
686 if (!sg_cnt) {
687 rval = -ENOMEM;
688 goto exit_mgmt;
689 }
690
691 dma_direction = DMA_FROM_DEVICE;
692
693 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
694 DEBUG2(printk(KERN_INFO
695 "dma mapping resulted in different sg counts "
696 "reply_sg_cnt: %x dma_reply_sg_cnt: %x\n",
697 bsg_job->reply_payload.sg_cnt, sg_cnt));
698 rval = -EAGAIN;
699 goto done_unmap_sg;
700 }
701
702 data_len = bsg_job->reply_payload.payload_len;
703
704 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
705 &mgmt_dma, GFP_KERNEL);
706 if (!mgmt_b) {
707 DEBUG2(printk(KERN_ERR "%s: dma alloc for mgmt_b "
708 "failed for host=%lu\n",
709 __func__, vha->host_no));
710 rval = -ENOMEM;
711 goto done_unmap_sg;
712 }
713
714 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
715 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
716 mn->parameter1 =
717 cpu_to_le32(
718 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
719
720 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
721 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
722 mn->parameter1 =
723 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
724
725 mn->parameter2 =
726 cpu_to_le32(
727 ql84_mgmt->mgmt.mgmtp.u.info.context);
728 }
729 break;
730
731 case QLA84_MGMT_WRITE_MEM:
732 sg_cnt = dma_map_sg(&ha->pdev->dev,
733 bsg_job->request_payload.sg_list,
734 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
735
736 if (!sg_cnt) {
737 rval = -ENOMEM;
738 goto exit_mgmt;
739 }
740
741 dma_direction = DMA_TO_DEVICE;
742
743 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
744 DEBUG2(printk(KERN_INFO
745 "dma mapping resulted in different sg counts "
746 "request_sg_cnt: %x dma_request_sg_cnt: %x ",
747 bsg_job->request_payload.sg_cnt, sg_cnt));
748 rval = -EAGAIN;
749 goto done_unmap_sg;
750 }
751
752 data_len = bsg_job->request_payload.payload_len;
753 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
754 &mgmt_dma, GFP_KERNEL);
755 if (!mgmt_b) {
756 DEBUG2(printk(KERN_ERR "%s: dma alloc for mgmt_b "
757 "failed for host=%lu\n",
758 __func__, vha->host_no));
759 rval = -ENOMEM;
760 goto done_unmap_sg;
761 }
762
763 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
764 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
765
766 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
767 mn->parameter1 =
768 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
769 break;
770
771 case QLA84_MGMT_CHNG_CONFIG:
772 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
773 mn->parameter1 =
774 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
775
776 mn->parameter2 =
777 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
778
779 mn->parameter3 =
780 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
781 break;
782
783 default:
784 rval = -EIO;
785 goto exit_mgmt;
786 }
787
788 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
789 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
790 mn->dseg_count = cpu_to_le16(1);
791 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
792 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
793 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
794 }
795
796 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
797
798 if (rval) {
799 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
800 "request 84xx mgmt failed\n", vha->host_no));
801
802 rval = bsg_job->reply->reply_payload_rcv_len = 0;
803 bsg_job->reply->result = (DID_ERROR << 16);
804
805 } else {
806 DEBUG2(qla_printk(KERN_WARNING, ha, "scsi(%ld) Vendor "
807 "request 84xx mgmt completed\n", vha->host_no));
808
809 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
810 bsg_job->reply->result = DID_OK;
811
812 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
813 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
814 bsg_job->reply->reply_payload_rcv_len =
815 bsg_job->reply_payload.payload_len;
816
817 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
818 bsg_job->reply_payload.sg_cnt, mgmt_b, data_len);
819 }
820 }
821
822 bsg_job->job_done(bsg_job);
823 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
824
825done_unmap_sg:
826 if (dma_direction == DMA_TO_DEVICE)
827 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
828 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
829 else if (dma_direction == DMA_FROM_DEVICE)
830 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
831 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
832
833exit_mgmt:
834 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
835
836 return rval;
837}
838
839static int
840qla24xx_iidma(struct fc_bsg_job *bsg_job)
841{
842 struct Scsi_Host *host = bsg_job->shost;
843 scsi_qla_host_t *vha = shost_priv(host);
844 struct qla_hw_data *ha = vha->hw;
845 int rval = 0;
846 struct qla_port_param *port_param = NULL;
847 fc_port_t *fcport = NULL;
848 uint16_t mb[MAILBOX_REGISTER_COUNT];
849 uint8_t *rsp_ptr = NULL;
850
851 bsg_job->reply->reply_payload_rcv_len = 0;
852
853 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
854 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
855 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags))
856 return -EBUSY;
857
858 if (!IS_IIDMA_CAPABLE(vha->hw)) {
859 DEBUG2(qla_printk(KERN_WARNING, ha, "%s(%lu): iiDMA not "
860 "supported\n", __func__, vha->host_no));
861 return -EINVAL;
862 }
863
864 port_param = (struct qla_port_param *)((char *)bsg_job->request +
865 sizeof(struct fc_bsg_request));
866 if (!port_param) {
867 DEBUG2(printk("%s(%ld): port_param header not provided, "
868 "exiting.\n", __func__, vha->host_no));
869 return -EINVAL;
870 }
871
872 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
873 DEBUG2(printk(KERN_ERR "%s(%ld): Invalid destination type\n",
874 __func__, vha->host_no));
875 return -EINVAL;
876 }
877
878 list_for_each_entry(fcport, &vha->vp_fcports, list) {
879 if (fcport->port_type != FCT_TARGET)
880 continue;
881
882 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
883 fcport->port_name, sizeof(fcport->port_name)))
884 continue;
885 break;
886 }
887
888 if (!fcport) {
889 DEBUG2(printk(KERN_ERR "%s(%ld): Failed to find port\n",
890 __func__, vha->host_no));
891 return -EINVAL;
892 }
893
894 if (port_param->mode)
895 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
896 port_param->speed, mb);
897 else
898 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
899 &port_param->speed, mb);
900
901 if (rval) {
902 DEBUG16(printk(KERN_ERR "scsi(%ld): iIDMA cmd failed for "
903 "%02x%02x%02x%02x%02x%02x%02x%02x -- %04x %x %04x %04x.\n",
904 vha->host_no, fcport->port_name[0],
905 fcport->port_name[1],
906 fcport->port_name[2], fcport->port_name[3],
907 fcport->port_name[4], fcport->port_name[5],
908 fcport->port_name[6], fcport->port_name[7], rval,
909 fcport->fp_speed, mb[0], mb[1]));
910 rval = 0;
911 bsg_job->reply->result = (DID_ERROR << 16);
912
913 } else {
914 if (!port_param->mode) {
915 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
916 sizeof(struct qla_port_param);
917
918 rsp_ptr = ((uint8_t *)bsg_job->reply) +
919 sizeof(struct fc_bsg_reply);
920
921 memcpy(rsp_ptr, port_param,
922 sizeof(struct qla_port_param));
923 }
924
925 bsg_job->reply->result = DID_OK;
926 }
927
928 bsg_job->job_done(bsg_job);
929 return rval;
930}
931
932static int
933qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
934{
935 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
936 case QL_VND_LOOPBACK:
937 return qla2x00_process_loopback(bsg_job);
938
939 case QL_VND_A84_RESET:
940 return qla84xx_reset(bsg_job);
941
942 case QL_VND_A84_UPDATE_FW:
943 return qla84xx_updatefw(bsg_job);
944
945 case QL_VND_A84_MGMT_CMD:
946 return qla84xx_mgmt_cmd(bsg_job);
947
948 case QL_VND_IIDMA:
949 return qla24xx_iidma(bsg_job);
950
951 default:
952 bsg_job->reply->result = (DID_ERROR << 16);
953 bsg_job->job_done(bsg_job);
954 return -ENOSYS;
955 }
956}
957
958int
959qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
960{
961 int ret = -EINVAL;
962
963 switch (bsg_job->request->msgcode) {
964 case FC_BSG_RPT_ELS:
965 case FC_BSG_HST_ELS_NOLOGIN:
966 ret = qla2x00_process_els(bsg_job);
967 break;
968 case FC_BSG_HST_CT:
969 ret = qla2x00_process_ct(bsg_job);
970 break;
971 case FC_BSG_HST_VENDOR:
972 ret = qla2x00_process_vendor_specific(bsg_job);
973 break;
974 case FC_BSG_HST_ADD_RPORT:
975 case FC_BSG_HST_DEL_RPORT:
976 case FC_BSG_RPT_CT:
977 default:
978 DEBUG2(printk("qla2xxx: unsupported BSG request\n"));
979 break;
980 }
981 return ret;
982}
983
984int
985qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
986{
987 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
988 struct qla_hw_data *ha = vha->hw;
989 srb_t *sp;
990 int cnt, que;
991 unsigned long flags;
992 struct req_que *req;
993 struct srb_bsg *sp_bsg;
994
995 /* find the bsg job from the active list of commands */
996 spin_lock_irqsave(&ha->hardware_lock, flags);
997 for (que = 0; que < ha->max_req_queues; que++) {
998 req = ha->req_q_map[que];
999 if (!req)
1000 continue;
1001
1002 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++ ) {
1003 sp = req->outstanding_cmds[cnt];
1004
1005 if (sp) {
1006 sp_bsg = (struct srb_bsg*)sp->ctx;
1007
1008 if (((sp_bsg->ctx.type == SRB_CT_CMD) ||
1009 (sp_bsg->ctx.type == SRB_ELS_CMD_HST))
1010 && (sp_bsg->bsg_job == bsg_job)) {
1011 if (ha->isp_ops->abort_command(sp)) {
1012 DEBUG2(qla_printk(KERN_INFO, ha,
1013 "scsi(%ld): mbx abort_command failed\n", vha->host_no));
1014 bsg_job->req->errors =
1015 bsg_job->reply->result = -EIO;
1016 } else {
1017 DEBUG2(qla_printk(KERN_INFO, ha,
1018 "scsi(%ld): mbx abort_command success\n", vha->host_no));
1019 bsg_job->req->errors =
1020 bsg_job->reply->result = 0;
1021 }
1022 goto done;
1023 }
1024 }
1025 }
1026 }
1027 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1028 DEBUG2(qla_printk(KERN_INFO, ha,
1029 "scsi(%ld) SRB not found to abort\n", vha->host_no));
1030 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
1031 return 0;
1032
1033done:
1034 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1035 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
1036 kfree(sp->fcport);
1037 kfree(sp->ctx);
1038 mempool_free(sp, ha->srb_mempool);
1039 return 0;
1040}