ql4_bsg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * QLogic iSCSI HBA Driver
  3. * Copyright (c) 2011 QLogic Corporation
  4. *
  5. * See LICENSE.qla4xxx for copyright and licensing details.
  6. */
  7. #include "ql4_def.h"
  8. #include "ql4_glbl.h"
  9. #include "ql4_bsg.h"
  10. static int
  11. qla4xxx_read_flash(struct bsg_job *bsg_job)
  12. {
  13. struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
  14. struct scsi_qla_host *ha = to_qla_host(host);
  15. struct iscsi_bsg_reply *bsg_reply = bsg_job->reply;
  16. struct iscsi_bsg_request *bsg_req = bsg_job->request;
  17. uint32_t offset = 0;
  18. uint32_t length = 0;
  19. dma_addr_t flash_dma;
  20. uint8_t *flash = NULL;
  21. int rval = -EINVAL;
  22. bsg_reply->reply_payload_rcv_len = 0;
  23. if (unlikely(pci_channel_offline(ha->pdev)))
  24. goto leave;
  25. if (ql4xxx_reset_active(ha)) {
  26. ql4_printk(KERN_ERR, ha, "%s: reset active\n", __func__);
  27. rval = -EBUSY;
  28. goto leave;
  29. }
  30. if (ha->flash_state != QLFLASH_WAITING) {
  31. ql4_printk(KERN_ERR, ha, "%s: another flash operation "
  32. "active\n", __func__);
  33. rval = -EBUSY;
  34. goto leave;
  35. }
  36. ha->flash_state = QLFLASH_READING;
  37. offset = bsg_req->rqst_data.h_vendor.vendor_cmd[1];
  38. length = bsg_job->reply_payload.payload_len;
  39. flash = dma_alloc_coherent(&ha->pdev->dev, length, &flash_dma,
  40. GFP_KERNEL);
  41. if (!flash) {
  42. ql4_printk(KERN_ERR, ha, "%s: dma alloc failed for flash "
  43. "data\n", __func__);
  44. rval = -ENOMEM;
  45. goto leave;
  46. }
  47. rval = qla4xxx_get_flash(ha, flash_dma, offset, length);
  48. if (rval) {
  49. ql4_printk(KERN_ERR, ha, "%s: get flash failed\n", __func__);
  50. bsg_reply->result = DID_ERROR << 16;
  51. rval = -EIO;
  52. } else {
  53. bsg_reply->reply_payload_rcv_len =
  54. sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
  55. bsg_job->reply_payload.sg_cnt,
  56. flash, length);
  57. bsg_reply->result = DID_OK << 16;
  58. }
  59. bsg_job_done(bsg_job, bsg_reply->result,
  60. bsg_reply->reply_payload_rcv_len);
  61. dma_free_coherent(&ha->pdev->dev, length, flash, flash_dma);
  62. leave:
  63. ha->flash_state = QLFLASH_WAITING;
  64. return rval;
  65. }
  66. static int
  67. qla4xxx_update_flash(struct bsg_job *bsg_job)
  68. {
  69. struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
  70. struct scsi_qla_host *ha = to_qla_host(host);
  71. struct iscsi_bsg_reply *bsg_reply = bsg_job->reply;
  72. struct iscsi_bsg_request *bsg_req = bsg_job->request;
  73. uint32_t length = 0;
  74. uint32_t offset = 0;
  75. uint32_t options = 0;
  76. dma_addr_t flash_dma;
  77. uint8_t *flash = NULL;
  78. int rval = -EINVAL;
  79. bsg_reply->reply_payload_rcv_len = 0;
  80. if (unlikely(pci_channel_offline(ha->pdev)))
  81. goto leave;
  82. if (ql4xxx_reset_active(ha)) {
  83. ql4_printk(KERN_ERR, ha, "%s: reset active\n", __func__);
  84. rval = -EBUSY;
  85. goto leave;
  86. }
  87. if (ha->flash_state != QLFLASH_WAITING) {
  88. ql4_printk(KERN_ERR, ha, "%s: another flash operation "
  89. "active\n", __func__);
  90. rval = -EBUSY;
  91. goto leave;
  92. }
  93. ha->flash_state = QLFLASH_WRITING;
  94. length = bsg_job->request_payload.payload_len;
  95. offset = bsg_req->rqst_data.h_vendor.vendor_cmd[1];
  96. options = bsg_req->rqst_data.h_vendor.vendor_cmd[2];
  97. flash = dma_alloc_coherent(&ha->pdev->dev, length, &flash_dma,
  98. GFP_KERNEL);
  99. if (!flash) {
  100. ql4_printk(KERN_ERR, ha, "%s: dma alloc failed for flash "
  101. "data\n", __func__);
  102. rval = -ENOMEM;
  103. goto leave;
  104. }
  105. sg_copy_to_buffer(bsg_job->request_payload.sg_list,
  106. bsg_job->request_payload.sg_cnt, flash, length);
  107. rval = qla4xxx_set_flash(ha, flash_dma, offset, length, options);
  108. if (rval) {
  109. ql4_printk(KERN_ERR, ha, "%s: set flash failed\n", __func__);
  110. bsg_reply->result = DID_ERROR << 16;
  111. rval = -EIO;
  112. } else
  113. bsg_reply->result = DID_OK << 16;
  114. bsg_job_done(bsg_job, bsg_reply->result,
  115. bsg_reply->reply_payload_rcv_len);
  116. dma_free_coherent(&ha->pdev->dev, length, flash, flash_dma);
  117. leave:
  118. ha->flash_state = QLFLASH_WAITING;
  119. return rval;
  120. }
  121. /**
  122. * qla4xxx_process_vendor_specific - handle vendor specific bsg request
  123. * @job: iscsi_bsg_job to handle
  124. **/
  125. int qla4xxx_process_vendor_specific(struct bsg_job *bsg_job)
  126. {
  127. struct iscsi_bsg_reply *bsg_reply = bsg_job->reply;
  128. struct iscsi_bsg_request *bsg_req = bsg_job->request;
  129. struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
  130. struct scsi_qla_host *ha = to_qla_host(host);
  131. switch (bsg_req->rqst_data.h_vendor.vendor_cmd[0]) {
  132. case QLISCSI_VND_READ_FLASH:
  133. return qla4xxx_read_flash(bsg_job);
  134. case QLISCSI_VND_UPDATE_FLASH:
  135. return qla4xxx_update_flash(bsg_job);
  136. default:
  137. ql4_printk(KERN_ERR, ha, "%s: invalid BSG vendor command: "
  138. "0x%x\n", __func__, bsg_req->msgcode);
  139. bsg_reply->result = (DID_ERROR << 16);
  140. bsg_reply->reply_payload_rcv_len = 0;
  141. bsg_job_done(bsg_job, bsg_reply->result,
  142. bsg_reply->reply_payload_rcv_len);
  143. return -ENOSYS;
  144. }
  145. }
  146. /**
  147. * qla4xxx_bsg_request - handle bsg request from ISCSI transport
  148. * @job: iscsi_bsg_job to handle
  149. */
  150. int qla4xxx_bsg_request(struct bsg_job *bsg_job)
  151. {
  152. struct iscsi_bsg_request *bsg_req = bsg_job->request;
  153. struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
  154. struct scsi_qla_host *ha = to_qla_host(host);
  155. switch (bsg_req->msgcode) {
  156. case ISCSI_BSG_HST_VENDOR:
  157. return qla4xxx_process_vendor_specific(bsg_job);
  158. default:
  159. ql4_printk(KERN_ERR, ha, "%s: invalid BSG command: 0x%x\n",
  160. __func__, bsg_req->msgcode);
  161. }
  162. return -ENOSYS;
  163. }