zfcp_fc.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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, refers to embedded @u submember
  111. * @sg_rsp: scatterlist entry for response data, refers to embedded @u submember
  112. * @u: request and response specific data
  113. * @u.adisc: ADISC specific data
  114. * @u.adisc.req: ADISC request
  115. * @u.adisc.rsp: ADISC response
  116. * @u.gid_pn: GID_PN specific data
  117. * @u.gid_pn.req: GID_PN request
  118. * @u.gid_pn.rsp: GID_PN response
  119. * @u.gpn_ft: GPN_FT specific data
  120. * @u.gpn_ft.sg_rsp2: GPN_FT response, not embedded here, allocated elsewhere
  121. * @u.gpn_ft.req: GPN_FT request
  122. * @u.gspn: GSPN specific data
  123. * @u.gspn.req: GSPN request
  124. * @u.gspn.rsp: GSPN response
  125. * @u.rspn: RSPN specific data
  126. * @u.rspn.req: RSPN request
  127. * @u.rspn.rsp: RSPN response
  128. */
  129. struct zfcp_fc_req {
  130. struct zfcp_fsf_ct_els ct_els;
  131. struct scatterlist sg_req;
  132. struct scatterlist sg_rsp;
  133. union {
  134. struct {
  135. struct fc_els_adisc req;
  136. struct fc_els_adisc rsp;
  137. } adisc;
  138. struct {
  139. struct zfcp_fc_gid_pn_req req;
  140. struct zfcp_fc_gid_pn_rsp rsp;
  141. } gid_pn;
  142. struct {
  143. struct scatterlist sg_rsp2[ZFCP_FC_GPN_FT_NUM_BUFS - 1];
  144. struct zfcp_fc_gpn_ft_req req;
  145. } gpn_ft;
  146. struct {
  147. struct zfcp_fc_gspn_req req;
  148. struct zfcp_fc_gspn_rsp rsp;
  149. } gspn;
  150. struct {
  151. struct zfcp_fc_rspn_req req;
  152. struct fc_ct_hdr rsp;
  153. } rspn;
  154. } u;
  155. };
  156. /**
  157. * enum zfcp_fc_wka_status - FC WKA port status in zfcp
  158. * @ZFCP_FC_WKA_PORT_OFFLINE: Port is closed and not in use
  159. * @ZFCP_FC_WKA_PORT_CLOSING: The FSF "close port" request is pending
  160. * @ZFCP_FC_WKA_PORT_OPENING: The FSF "open port" request is pending
  161. * @ZFCP_FC_WKA_PORT_ONLINE: The port is open and the port handle is valid
  162. */
  163. enum zfcp_fc_wka_status {
  164. ZFCP_FC_WKA_PORT_OFFLINE,
  165. ZFCP_FC_WKA_PORT_CLOSING,
  166. ZFCP_FC_WKA_PORT_OPENING,
  167. ZFCP_FC_WKA_PORT_ONLINE,
  168. };
  169. /**
  170. * struct zfcp_fc_wka_port - representation of well-known-address (WKA) FC port
  171. * @adapter: Pointer to adapter structure this WKA port belongs to
  172. * @opened: Wait for completion of open command
  173. * @closed: Wait for completion of close command
  174. * @status: Current status of WKA port
  175. * @refcount: Reference count to keep port open as long as it is in use
  176. * @d_id: FC destination id or well-known-address
  177. * @handle: FSF handle for the open WKA port
  178. * @mutex: Mutex used during opening/closing state changes
  179. * @work: For delaying the closing of the WKA port
  180. */
  181. struct zfcp_fc_wka_port {
  182. struct zfcp_adapter *adapter;
  183. wait_queue_head_t opened;
  184. wait_queue_head_t closed;
  185. enum zfcp_fc_wka_status status;
  186. atomic_t refcount;
  187. u32 d_id;
  188. u32 handle;
  189. struct mutex mutex;
  190. struct delayed_work work;
  191. };
  192. /**
  193. * struct zfcp_fc_wka_ports - Data structures for FC generic services
  194. * @ms: FC Management service
  195. * @ts: FC time service
  196. * @ds: FC directory service
  197. * @as: FC alias service
  198. */
  199. struct zfcp_fc_wka_ports {
  200. struct zfcp_fc_wka_port ms;
  201. struct zfcp_fc_wka_port ts;
  202. struct zfcp_fc_wka_port ds;
  203. struct zfcp_fc_wka_port as;
  204. };
  205. /**
  206. * zfcp_fc_scsi_to_fcp - setup FCP command with data from scsi_cmnd
  207. * @fcp: fcp_cmnd to setup
  208. * @scsi: scsi_cmnd where to get LUN, task attributes/flags and CDB
  209. */
  210. static inline
  211. void zfcp_fc_scsi_to_fcp(struct fcp_cmnd *fcp, struct scsi_cmnd *scsi)
  212. {
  213. u32 datalen;
  214. int_to_scsilun(scsi->device->lun, (struct scsi_lun *) &fcp->fc_lun);
  215. fcp->fc_pri_ta = FCP_PTA_SIMPLE;
  216. if (scsi->sc_data_direction == DMA_FROM_DEVICE)
  217. fcp->fc_flags |= FCP_CFL_RDDATA;
  218. if (scsi->sc_data_direction == DMA_TO_DEVICE)
  219. fcp->fc_flags |= FCP_CFL_WRDATA;
  220. memcpy(fcp->fc_cdb, scsi->cmnd, scsi->cmd_len);
  221. datalen = scsi_bufflen(scsi);
  222. fcp->fc_dl = cpu_to_be32(datalen);
  223. if (scsi_get_prot_type(scsi) == SCSI_PROT_DIF_TYPE1) {
  224. datalen += datalen / scsi->device->sector_size * 8;
  225. fcp->fc_dl = cpu_to_be32(datalen);
  226. }
  227. }
  228. /**
  229. * zfcp_fc_fcp_tm() - Setup FCP command as task management command.
  230. * @fcp: Pointer to FCP_CMND IU to set up.
  231. * @dev: Pointer to SCSI_device where to send the task management command.
  232. * @tm_flags: Task management flags to setup tm command.
  233. */
  234. static inline
  235. void zfcp_fc_fcp_tm(struct fcp_cmnd *fcp, struct scsi_device *dev, u8 tm_flags)
  236. {
  237. int_to_scsilun(dev->lun, (struct scsi_lun *) &fcp->fc_lun);
  238. fcp->fc_tm_flags = tm_flags;
  239. }
  240. /**
  241. * zfcp_fc_evap_fcp_rsp - evaluate FCP RSP IU and update scsi_cmnd accordingly
  242. * @fcp_rsp: FCP RSP IU to evaluate
  243. * @scsi: SCSI command where to update status and sense buffer
  244. */
  245. static inline
  246. void zfcp_fc_eval_fcp_rsp(struct fcp_resp_with_ext *fcp_rsp,
  247. struct scsi_cmnd *scsi)
  248. {
  249. struct fcp_resp_rsp_info *rsp_info;
  250. char *sense;
  251. u32 sense_len, resid;
  252. u8 rsp_flags;
  253. scsi->result |= fcp_rsp->resp.fr_status;
  254. rsp_flags = fcp_rsp->resp.fr_flags;
  255. if (unlikely(rsp_flags & FCP_RSP_LEN_VAL)) {
  256. rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
  257. if (rsp_info->rsp_code == FCP_TMF_CMPL)
  258. set_host_byte(scsi, DID_OK);
  259. else {
  260. set_host_byte(scsi, DID_ERROR);
  261. return;
  262. }
  263. }
  264. if (unlikely(rsp_flags & FCP_SNS_LEN_VAL)) {
  265. sense = (char *) &fcp_rsp[1];
  266. if (rsp_flags & FCP_RSP_LEN_VAL)
  267. sense += be32_to_cpu(fcp_rsp->ext.fr_rsp_len);
  268. sense_len = min_t(u32, be32_to_cpu(fcp_rsp->ext.fr_sns_len),
  269. SCSI_SENSE_BUFFERSIZE);
  270. memcpy(scsi->sense_buffer, sense, sense_len);
  271. }
  272. if (unlikely(rsp_flags & FCP_RESID_UNDER)) {
  273. resid = be32_to_cpu(fcp_rsp->ext.fr_resid);
  274. scsi_set_resid(scsi, resid);
  275. if (scsi_bufflen(scsi) - resid < scsi->underflow &&
  276. !(rsp_flags & FCP_SNS_LEN_VAL) &&
  277. fcp_rsp->resp.fr_status == SAM_STAT_GOOD)
  278. set_host_byte(scsi, DID_ERROR);
  279. } else if (unlikely(rsp_flags & FCP_RESID_OVER)) {
  280. /* FCP_DL was not sufficient for SCSI data length */
  281. if (fcp_rsp->resp.fr_status == SAM_STAT_GOOD)
  282. set_host_byte(scsi, DID_ERROR);
  283. }
  284. }
  285. #endif