1 /* 2 * zfcp device driver 3 * 4 * Fibre Channel related definitions and inline functions for the zfcp 5 * device driver 6 * 7 * Copyright IBM Corporation 2009 8 */ 9 10 #ifndef ZFCP_FC_H 11 #define ZFCP_FC_H 12 13 #include <scsi/fc/fc_els.h> 14 #include <scsi/fc/fc_fcp.h> 15 #include <scsi/fc/fc_ns.h> 16 #include <scsi/scsi_cmnd.h> 17 #include <scsi/scsi_tcq.h> 18 #include "zfcp_fsf.h" 19 20 #define ZFCP_FC_CT_SIZE_PAGE (PAGE_SIZE - sizeof(struct fc_ct_hdr)) 21 #define ZFCP_FC_GPN_FT_ENT_PAGE (ZFCP_FC_CT_SIZE_PAGE \ 22 / sizeof(struct fc_gpn_ft_resp)) 23 #define ZFCP_FC_GPN_FT_NUM_BUFS 4 /* memory pages */ 24 25 #define ZFCP_FC_GPN_FT_MAX_SIZE (ZFCP_FC_GPN_FT_NUM_BUFS * PAGE_SIZE \ 26 - sizeof(struct fc_ct_hdr)) 27 #define ZFCP_FC_GPN_FT_MAX_ENT (ZFCP_FC_GPN_FT_NUM_BUFS * \ 28 (ZFCP_FC_GPN_FT_ENT_PAGE + 1)) 29 30 /** 31 * struct zfcp_fc_gid_pn_req - container for ct header plus gid_pn request 32 * @ct_hdr: FC GS common transport header 33 * @gid_pn: GID_PN request 34 */ 35 struct zfcp_fc_gid_pn_req { 36 struct fc_ct_hdr ct_hdr; 37 struct fc_ns_gid_pn gid_pn; 38 } __packed; 39 40 /** 41 * struct zfcp_fc_gid_pn_resp - container for ct header plus gid_pn response 42 * @ct_hdr: FC GS common transport header 43 * @gid_pn: GID_PN response 44 */ 45 struct zfcp_fc_gid_pn_resp { 46 struct fc_ct_hdr ct_hdr; 47 struct fc_gid_pn_resp gid_pn; 48 } __packed; 49 50 /** 51 * struct zfcp_fc_gid_pn - everything required in zfcp for gid_pn request 52 * @ct: data passed to zfcp_fsf for issuing fsf request 53 * @sg_req: scatterlist entry for request data 54 * @sg_resp: scatterlist entry for response data 55 * @gid_pn_req: GID_PN request data 56 * @gid_pn_resp: GID_PN response data 57 */ 58 struct zfcp_fc_gid_pn { 59 struct zfcp_fsf_ct_els ct; 60 struct scatterlist sg_req; 61 struct scatterlist sg_resp; 62 struct zfcp_fc_gid_pn_req gid_pn_req; 63 struct zfcp_fc_gid_pn_resp gid_pn_resp; 64 struct zfcp_port *port; 65 }; 66 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 /** 78 * struct zfcp_fc_gpn_ft_resp - container for ct header plus gpn_ft response 79 * @ct_hdr: FC GS common transport header 80 * @gpn_ft: Array of gpn_ft response data to fill one memory page 81 */ 82 struct zfcp_fc_gpn_ft_resp { 83 struct fc_ct_hdr ct_hdr; 84 struct fc_gpn_ft_resp gpn_ft[ZFCP_FC_GPN_FT_ENT_PAGE]; 85 } __packed; 86 87 /** 88 * struct zfcp_fc_gpn_ft - zfcp data for gpn_ft request 89 * @ct: data passed to zfcp_fsf for issuing fsf request 90 * @sg_req: scatter list entry for gpn_ft request 91 * @sg_resp: scatter list entries for gpn_ft responses (per memory page) 92 */ 93 struct zfcp_fc_gpn_ft { 94 struct zfcp_fsf_ct_els ct; 95 struct scatterlist sg_req; 96 struct scatterlist sg_resp[ZFCP_FC_GPN_FT_NUM_BUFS]; 97 }; 98 99 /** 100 * struct zfcp_fc_els_adisc - everything required in zfcp for issuing ELS ADISC 101 * @els: data required for issuing els fsf command 102 * @req: scatterlist entry for ELS ADISC request 103 * @resp: scatterlist entry for ELS ADISC response 104 * @adisc_req: ELS ADISC request data 105 * @adisc_resp: ELS ADISC response data 106 */ 107 struct zfcp_fc_els_adisc { 108 struct zfcp_fsf_ct_els els; 109 struct scatterlist req; 110 struct scatterlist resp; 111 struct fc_els_adisc adisc_req; 112 struct fc_els_adisc adisc_resp; 113 }; 114 115 /** 116 * enum zfcp_fc_wka_status - FC WKA port status in zfcp 117 * @ZFCP_FC_WKA_PORT_OFFLINE: Port is closed and not in use 118 * @ZFCP_FC_WKA_PORT_CLOSING: The FSF "close port" request is pending 119 * @ZFCP_FC_WKA_PORT_OPENING: The FSF "open port" request is pending 120 * @ZFCP_FC_WKA_PORT_ONLINE: The port is open and the port handle is valid 121 */ 122 enum zfcp_fc_wka_status { 123 ZFCP_FC_WKA_PORT_OFFLINE, 124 ZFCP_FC_WKA_PORT_CLOSING, 125 ZFCP_FC_WKA_PORT_OPENING, 126 ZFCP_FC_WKA_PORT_ONLINE, 127 }; 128 129 /** 130 * struct zfcp_fc_wka_port - representation of well-known-address (WKA) FC port 131 * @adapter: Pointer to adapter structure this WKA port belongs to 132 * @completion_wq: Wait for completion of open/close command 133 * @status: Current status of WKA port 134 * @refcount: Reference count to keep port open as long as it is in use 135 * @d_id: FC destination id or well-known-address 136 * @handle: FSF handle for the open WKA port 137 * @mutex: Mutex used during opening/closing state changes 138 * @work: For delaying the closing of the WKA port 139 */ 140 struct zfcp_fc_wka_port { 141 struct zfcp_adapter *adapter; 142 wait_queue_head_t completion_wq; 143 enum zfcp_fc_wka_status status; 144 atomic_t refcount; 145 u32 d_id; 146 u32 handle; 147 struct mutex mutex; 148 struct delayed_work work; 149 }; 150 151 /** 152 * struct zfcp_fc_wka_ports - Data structures for FC generic services 153 * @ms: FC Management service 154 * @ts: FC time service 155 * @ds: FC directory service 156 * @as: FC alias service 157 */ 158 struct zfcp_fc_wka_ports { 159 struct zfcp_fc_wka_port ms; 160 struct zfcp_fc_wka_port ts; 161 struct zfcp_fc_wka_port ds; 162 struct zfcp_fc_wka_port as; 163 }; 164 165 /** 166 * zfcp_fc_scsi_to_fcp - setup FCP command with data from scsi_cmnd 167 * @fcp: fcp_cmnd to setup 168 * @scsi: scsi_cmnd where to get LUN, task attributes/flags and CDB 169 */ 170 static inline 171 void zfcp_fc_scsi_to_fcp(struct fcp_cmnd *fcp, struct scsi_cmnd *scsi) 172 { 173 char tag[2]; 174 175 int_to_scsilun(scsi->device->lun, (struct scsi_lun *) &fcp->fc_lun); 176 177 if (scsi_populate_tag_msg(scsi, tag)) { 178 switch (tag[0]) { 179 case MSG_ORDERED_TAG: 180 fcp->fc_pri_ta |= FCP_PTA_ORDERED; 181 break; 182 case MSG_SIMPLE_TAG: 183 fcp->fc_pri_ta |= FCP_PTA_SIMPLE; 184 break; 185 }; 186 } else 187 fcp->fc_pri_ta = FCP_PTA_SIMPLE; 188 189 if (scsi->sc_data_direction == DMA_FROM_DEVICE) 190 fcp->fc_flags |= FCP_CFL_RDDATA; 191 if (scsi->sc_data_direction == DMA_TO_DEVICE) 192 fcp->fc_flags |= FCP_CFL_WRDATA; 193 194 memcpy(fcp->fc_cdb, scsi->cmnd, scsi->cmd_len); 195 196 fcp->fc_dl = scsi_bufflen(scsi); 197 } 198 199 /** 200 * zfcp_fc_fcp_tm - setup FCP command as task management command 201 * @fcp: fcp_cmnd to setup 202 * @dev: scsi_device where to send the task management command 203 * @tm: task management flags to setup tm command 204 */ 205 static inline 206 void zfcp_fc_fcp_tm(struct fcp_cmnd *fcp, struct scsi_device *dev, u8 tm_flags) 207 { 208 int_to_scsilun(dev->lun, (struct scsi_lun *) &fcp->fc_lun); 209 fcp->fc_tm_flags |= tm_flags; 210 } 211 212 /** 213 * zfcp_fc_evap_fcp_rsp - evaluate FCP RSP IU and update scsi_cmnd accordingly 214 * @fcp_rsp: FCP RSP IU to evaluate 215 * @scsi: SCSI command where to update status and sense buffer 216 */ 217 static inline 218 void zfcp_fc_eval_fcp_rsp(struct fcp_resp_with_ext *fcp_rsp, 219 struct scsi_cmnd *scsi) 220 { 221 struct fcp_resp_rsp_info *rsp_info; 222 char *sense; 223 u32 sense_len, resid; 224 u8 rsp_flags; 225 226 set_msg_byte(scsi, COMMAND_COMPLETE); 227 scsi->result |= fcp_rsp->resp.fr_status; 228 229 rsp_flags = fcp_rsp->resp.fr_flags; 230 231 if (unlikely(rsp_flags & FCP_RSP_LEN_VAL)) { 232 rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1]; 233 if (rsp_info->rsp_code == FCP_TMF_CMPL) 234 set_host_byte(scsi, DID_OK); 235 else { 236 set_host_byte(scsi, DID_ERROR); 237 return; 238 } 239 } 240 241 if (unlikely(rsp_flags & FCP_SNS_LEN_VAL)) { 242 sense = (char *) &fcp_rsp[1]; 243 if (rsp_flags & FCP_RSP_LEN_VAL) 244 sense += fcp_rsp->ext.fr_sns_len; 245 sense_len = min(fcp_rsp->ext.fr_sns_len, 246 (u32) SCSI_SENSE_BUFFERSIZE); 247 memcpy(scsi->sense_buffer, sense, sense_len); 248 } 249 250 if (unlikely(rsp_flags & FCP_RESID_UNDER)) { 251 resid = 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 } 258 } 259 260 #endif 261