zfcp_fc.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * zfcp device driver
  4. *
  5. * Fibre Channel related definitions and inline functions for the zfcp
  6. * device driver
  7. *
  8. * Copyright IBM Corp. 2009, 2017
  9. */
  10. #ifndef ZFCP_FC_H
  11. #define ZFCP_FC_H
  12. #include <scsi/fc/fc_els.h>
  13. #include <scsi/fc/fc_fcp.h>
  14. #include <scsi/fc/fc_ns.h>
  15. #include <scsi/scsi_cmnd.h>
  16. #include <scsi/scsi_tcq.h>
  17. #include "zfcp_fsf.h"
  18. #define ZFCP_FC_CT_SIZE_PAGE (PAGE_SIZE - sizeof(struct fc_ct_hdr))
  19. #define ZFCP_FC_GPN_FT_ENT_PAGE (ZFCP_FC_CT_SIZE_PAGE \
  20. / sizeof(struct fc_gpn_ft_resp))
  21. #define ZFCP_FC_GPN_FT_NUM_BUFS 4 /* memory pages */
  22. #define ZFCP_FC_GPN_FT_MAX_SIZE (ZFCP_FC_GPN_FT_NUM_BUFS * PAGE_SIZE \
  23. - sizeof(struct fc_ct_hdr))
  24. #define ZFCP_FC_GPN_FT_MAX_ENT (ZFCP_FC_GPN_FT_NUM_BUFS * \
  25. (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
  26. #define ZFCP_FC_CTELS_TMO (2 * FC_DEF_R_A_TOV / 1000)
  27. /**
  28. * struct zfcp_fc_event - FC HBAAPI event for internal queueing from irq context
  29. * @code: Event code
  30. * @data: Event data
  31. * @list: list_head for zfcp_fc_events list
  32. */
  33. struct zfcp_fc_event {
  34. enum fc_host_event_code code;
  35. u32 data;
  36. struct list_head list;
  37. };
  38. /**
  39. * struct zfcp_fc_events - Infrastructure for posting FC events from irq context
  40. * @list: List for queueing of events from irq context to workqueue
  41. * @list_lock: Lock for event list
  42. * @work: work_struct for forwarding events in workqueue
  43. */
  44. struct zfcp_fc_events {
  45. struct list_head list;
  46. spinlock_t list_lock;
  47. struct work_struct work;
  48. };
  49. /**
  50. * struct zfcp_fc_gid_pn_req - container for ct header plus gid_pn request
  51. * @ct_hdr: FC GS common transport header
  52. * @gid_pn: GID_PN request
  53. */
  54. struct zfcp_fc_gid_pn_req {
  55. struct fc_ct_hdr ct_hdr;
  56. struct fc_ns_gid_pn gid_pn;
  57. } __packed;
  58. /**
  59. * struct zfcp_fc_gid_pn_rsp - container for ct header plus gid_pn response
  60. * @ct_hdr: FC GS common transport header
  61. * @gid_pn: GID_PN response
  62. */
  63. struct zfcp_fc_gid_pn_rsp {
  64. struct fc_ct_hdr ct_hdr;
  65. struct fc_gid_pn_resp gid_pn;
  66. } __packed;
  67. /**
  68. * struct zfcp_fc_gpn_ft - container for ct header plus gpn_ft request
  69. * @ct_hdr: FC GS common transport header
  70. * @gpn_ft: GPN_FT request
  71. */
  72. struct zfcp_fc_gpn_ft_req {
  73. struct fc_ct_hdr ct_hdr;
  74. struct fc_ns_gid_ft gpn_ft;
  75. } __packed;
  76. /**
  77. * struct zfcp_fc_gspn_req - container for ct header plus GSPN_ID request
  78. * @ct_hdr: FC GS common transport header
  79. * @gspn: GSPN_ID request
  80. */
  81. struct zfcp_fc_gspn_req {
  82. struct fc_ct_hdr ct_hdr;
  83. struct fc_gid_pn_resp gspn;
  84. } __packed;
  85. /**
  86. * struct zfcp_fc_gspn_rsp - container for ct header plus GSPN_ID response
  87. * @ct_hdr: FC GS common transport header
  88. * @gspn: GSPN_ID response
  89. * @name: The name string of the GSPN_ID response
  90. */
  91. struct zfcp_fc_gspn_rsp {
  92. struct fc_ct_hdr ct_hdr;
  93. struct fc_gspn_resp gspn;
  94. char name[FC_SYMBOLIC_NAME_SIZE];
  95. } __packed;
  96. /**
  97. * struct zfcp_fc_rspn_req - container for ct header plus RSPN_ID request
  98. * @ct_hdr: FC GS common transport header
  99. * @rspn: RSPN_ID request
  100. * @name: The name string of the RSPN_ID request
  101. */
  102. struct zfcp_fc_rspn_req {
  103. struct fc_ct_hdr ct_hdr;
  104. struct fc_ns_rspn rspn;
  105. char name[FC_SYMBOLIC_NAME_SIZE];
  106. } __packed;
  107. /**
  108. * struct zfcp_fc_req - Container for FC ELS and CT requests sent from zfcp
  109. * @ct_els: data required for issuing fsf command
  110. * @sg_req: scatterlist entry for request data
  111. * @sg_rsp: scatterlist entry for response data
  112. * @u: request specific data
  113. */
  114. struct zfcp_fc_req {
  115. struct zfcp_fsf_ct_els ct_els;
  116. struct scatterlist sg_req;
  117. struct scatterlist sg_rsp;
  118. union {
  119. struct {
  120. struct fc_els_adisc req;
  121. struct fc_els_adisc rsp;
  122. } adisc;
  123. struct {
  124. struct zfcp_fc_gid_pn_req req;
  125. struct zfcp_fc_gid_pn_rsp rsp;
  126. } gid_pn;
  127. struct {
  128. struct scatterlist sg_rsp2[ZFCP_FC_GPN_FT_NUM_BUFS - 1];
  129. struct zfcp_fc_gpn_ft_req req;
  130. } gpn_ft;
  131. struct {
  132. struct zfcp_fc_gspn_req req;
  133. struct zfcp_fc_gspn_rsp rsp;
  134. } gspn;
  135. struct {
  136. struct zfcp_fc_rspn_req req;
  137. struct fc_ct_hdr rsp;
  138. } rspn;
  139. } u;
  140. };
  141. /**
  142. * enum zfcp_fc_wka_status - FC WKA port status in zfcp
  143. * @ZFCP_FC_WKA_PORT_OFFLINE: Port is closed and not in use
  144. * @ZFCP_FC_WKA_PORT_CLOSING: The FSF "close port" request is pending
  145. * @ZFCP_FC_WKA_PORT_OPENING: The FSF "open port" request is pending
  146. * @ZFCP_FC_WKA_PORT_ONLINE: The port is open and the port handle is valid
  147. */
  148. enum zfcp_fc_wka_status {
  149. ZFCP_FC_WKA_PORT_OFFLINE,
  150. ZFCP_FC_WKA_PORT_CLOSING,
  151. ZFCP_FC_WKA_PORT_OPENING,
  152. ZFCP_FC_WKA_PORT_ONLINE,
  153. };
  154. /**
  155. * struct zfcp_fc_wka_port - representation of well-known-address (WKA) FC port
  156. * @adapter: Pointer to adapter structure this WKA port belongs to
  157. * @completion_wq: Wait for completion of open/close command
  158. * @status: Current status of WKA port
  159. * @refcount: Reference count to keep port open as long as it is in use
  160. * @d_id: FC destination id or well-known-address
  161. * @handle: FSF handle for the open WKA port
  162. * @mutex: Mutex used during opening/closing state changes
  163. * @work: For delaying the closing of the WKA port
  164. */
  165. struct zfcp_fc_wka_port {
  166. struct zfcp_adapter *adapter;
  167. wait_queue_head_t completion_wq;
  168. enum zfcp_fc_wka_status status;
  169. atomic_t refcount;
  170. u32 d_id;
  171. u32 handle;
  172. struct mutex mutex;
  173. struct delayed_work work;
  174. };
  175. /**
  176. * struct zfcp_fc_wka_ports - Data structures for FC generic services
  177. * @ms: FC Management service
  178. * @ts: FC time service
  179. * @ds: FC directory service
  180. * @as: FC alias service
  181. */
  182. struct zfcp_fc_wka_ports {
  183. struct zfcp_fc_wka_port ms;
  184. struct zfcp_fc_wka_port ts;
  185. struct zfcp_fc_wka_port ds;
  186. struct zfcp_fc_wka_port as;
  187. };
  188. /**
  189. * zfcp_fc_scsi_to_fcp - setup FCP command with data from scsi_cmnd
  190. * @fcp: fcp_cmnd to setup
  191. * @scsi: scsi_cmnd where to get LUN, task attributes/flags and CDB
  192. * @tm: task management flags to setup task management command
  193. */
  194. static inline
  195. void zfcp_fc_scsi_to_fcp(struct fcp_cmnd *fcp, struct scsi_cmnd *scsi,
  196. u8 tm_flags)
  197. {
  198. u32 datalen;
  199. int_to_scsilun(scsi->device->lun, (struct scsi_lun *) &fcp->fc_lun);
  200. if (unlikely(tm_flags)) {
  201. fcp->fc_tm_flags = tm_flags;
  202. return;
  203. }
  204. fcp->fc_pri_ta = FCP_PTA_SIMPLE;
  205. if (scsi->sc_data_direction == DMA_FROM_DEVICE)
  206. fcp->fc_flags |= FCP_CFL_RDDATA;
  207. if (scsi->sc_data_direction == DMA_TO_DEVICE)
  208. fcp->fc_flags |= FCP_CFL_WRDATA;
  209. memcpy(fcp->fc_cdb, scsi->cmnd, scsi->cmd_len);
  210. datalen = scsi_bufflen(scsi);
  211. fcp->fc_dl = cpu_to_be32(datalen);
  212. if (scsi_get_prot_type(scsi) == SCSI_PROT_DIF_TYPE1) {
  213. datalen += datalen / scsi->device->sector_size * 8;
  214. fcp->fc_dl = cpu_to_be32(datalen);
  215. }
  216. }
  217. /**
  218. * zfcp_fc_evap_fcp_rsp - evaluate FCP RSP IU and update scsi_cmnd accordingly
  219. * @fcp_rsp: FCP RSP IU to evaluate
  220. * @scsi: SCSI command where to update status and sense buffer
  221. */
  222. static inline
  223. void zfcp_fc_eval_fcp_rsp(struct fcp_resp_with_ext *fcp_rsp,
  224. struct scsi_cmnd *scsi)
  225. {
  226. struct fcp_resp_rsp_info *rsp_info;
  227. char *sense;
  228. u32 sense_len, resid;
  229. u8 rsp_flags;
  230. set_msg_byte(scsi, COMMAND_COMPLETE);
  231. scsi->result |= fcp_rsp->resp.fr_status;
  232. rsp_flags = fcp_rsp->resp.fr_flags;
  233. if (unlikely(rsp_flags & FCP_RSP_LEN_VAL)) {
  234. rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
  235. if (rsp_info->rsp_code == FCP_TMF_CMPL)
  236. set_host_byte(scsi, DID_OK);
  237. else {
  238. set_host_byte(scsi, DID_ERROR);
  239. return;
  240. }
  241. }
  242. if (unlikely(rsp_flags & FCP_SNS_LEN_VAL)) {
  243. sense = (char *) &fcp_rsp[1];
  244. if (rsp_flags & FCP_RSP_LEN_VAL)
  245. sense += be32_to_cpu(fcp_rsp->ext.fr_rsp_len);
  246. sense_len = min_t(u32, be32_to_cpu(fcp_rsp->ext.fr_sns_len),
  247. SCSI_SENSE_BUFFERSIZE);
  248. memcpy(scsi->sense_buffer, sense, sense_len);
  249. }
  250. if (unlikely(rsp_flags & FCP_RESID_UNDER)) {
  251. resid = be32_to_cpu(fcp_rsp->ext.fr_resid);
  252. scsi_set_resid(scsi, resid);
  253. if (scsi_bufflen(scsi) - resid < scsi->underflow &&
  254. !(rsp_flags & FCP_SNS_LEN_VAL) &&
  255. fcp_rsp->resp.fr_status == SAM_STAT_GOOD)
  256. set_host_byte(scsi, DID_ERROR);
  257. } else if (unlikely(rsp_flags & FCP_RESID_OVER)) {
  258. /* FCP_DL was not sufficient for SCSI data length */
  259. if (fcp_rsp->resp.fr_status == SAM_STAT_GOOD)
  260. set_host_byte(scsi, DID_ERROR);
  261. }
  262. }
  263. #endif