14318e08cSChristof Schmitt /* 24318e08cSChristof Schmitt * zfcp device driver 34318e08cSChristof Schmitt * 44318e08cSChristof Schmitt * Fibre Channel related definitions and inline functions for the zfcp 54318e08cSChristof Schmitt * device driver 64318e08cSChristof Schmitt * 74318e08cSChristof Schmitt * Copyright IBM Corporation 2009 84318e08cSChristof Schmitt */ 94318e08cSChristof Schmitt 104318e08cSChristof Schmitt #ifndef ZFCP_FC_H 114318e08cSChristof Schmitt #define ZFCP_FC_H 124318e08cSChristof Schmitt 139d05ce2cSChristof Schmitt #include <scsi/fc/fc_els.h> 144318e08cSChristof Schmitt #include <scsi/fc/fc_fcp.h> 15dbf5dfe9SChristof Schmitt #include <scsi/fc/fc_ns.h> 164318e08cSChristof Schmitt #include <scsi/scsi_cmnd.h> 174318e08cSChristof Schmitt #include <scsi/scsi_tcq.h> 187c7dc196SChristof Schmitt #include "zfcp_fsf.h" 194318e08cSChristof Schmitt 20dbf5dfe9SChristof Schmitt #define ZFCP_FC_CT_SIZE_PAGE (PAGE_SIZE - sizeof(struct fc_ct_hdr)) 21dbf5dfe9SChristof Schmitt #define ZFCP_FC_GPN_FT_ENT_PAGE (ZFCP_FC_CT_SIZE_PAGE \ 22dbf5dfe9SChristof Schmitt / sizeof(struct fc_gpn_ft_resp)) 23dbf5dfe9SChristof Schmitt #define ZFCP_FC_GPN_FT_NUM_BUFS 4 /* memory pages */ 24dbf5dfe9SChristof Schmitt 25dbf5dfe9SChristof Schmitt #define ZFCP_FC_GPN_FT_MAX_SIZE (ZFCP_FC_GPN_FT_NUM_BUFS * PAGE_SIZE \ 26dbf5dfe9SChristof Schmitt - sizeof(struct fc_ct_hdr)) 27dbf5dfe9SChristof Schmitt #define ZFCP_FC_GPN_FT_MAX_ENT (ZFCP_FC_GPN_FT_NUM_BUFS * \ 28dbf5dfe9SChristof Schmitt (ZFCP_FC_GPN_FT_ENT_PAGE + 1)) 29dbf5dfe9SChristof Schmitt 3051375ee8SSwen Schillig #define ZFCP_FC_CTELS_TMO (2 * FC_DEF_R_A_TOV / 1000) 3151375ee8SSwen Schillig 32dbf5dfe9SChristof Schmitt /** 332d1e547fSSven Schuetz * struct zfcp_fc_event - FC HBAAPI event for internal queueing from irq context 342d1e547fSSven Schuetz * @code: Event code 352d1e547fSSven Schuetz * @data: Event data 362d1e547fSSven Schuetz * @list: list_head for zfcp_fc_events list 372d1e547fSSven Schuetz */ 382d1e547fSSven Schuetz struct zfcp_fc_event { 392d1e547fSSven Schuetz enum fc_host_event_code code; 402d1e547fSSven Schuetz u32 data; 412d1e547fSSven Schuetz struct list_head list; 422d1e547fSSven Schuetz }; 432d1e547fSSven Schuetz 442d1e547fSSven Schuetz /** 452d1e547fSSven Schuetz * struct zfcp_fc_events - Infrastructure for posting FC events from irq context 462d1e547fSSven Schuetz * @list: List for queueing of events from irq context to workqueue 472d1e547fSSven Schuetz * @list_lock: Lock for event list 482d1e547fSSven Schuetz * @work: work_struct for forwarding events in workqueue 492d1e547fSSven Schuetz */ 502d1e547fSSven Schuetz struct zfcp_fc_events { 512d1e547fSSven Schuetz struct list_head list; 522d1e547fSSven Schuetz spinlock_t list_lock; 532d1e547fSSven Schuetz struct work_struct work; 542d1e547fSSven Schuetz }; 552d1e547fSSven Schuetz 562d1e547fSSven Schuetz /** 57dbf5dfe9SChristof Schmitt * struct zfcp_fc_gid_pn_req - container for ct header plus gid_pn request 58dbf5dfe9SChristof Schmitt * @ct_hdr: FC GS common transport header 59dbf5dfe9SChristof Schmitt * @gid_pn: GID_PN request 60dbf5dfe9SChristof Schmitt */ 61dbf5dfe9SChristof Schmitt struct zfcp_fc_gid_pn_req { 62dbf5dfe9SChristof Schmitt struct fc_ct_hdr ct_hdr; 63dbf5dfe9SChristof Schmitt struct fc_ns_gid_pn gid_pn; 64dbf5dfe9SChristof Schmitt } __packed; 65dbf5dfe9SChristof Schmitt 66dbf5dfe9SChristof Schmitt /** 67dbf5dfe9SChristof Schmitt * struct zfcp_fc_gid_pn_resp - container for ct header plus gid_pn response 68dbf5dfe9SChristof Schmitt * @ct_hdr: FC GS common transport header 69dbf5dfe9SChristof Schmitt * @gid_pn: GID_PN response 70dbf5dfe9SChristof Schmitt */ 71dbf5dfe9SChristof Schmitt struct zfcp_fc_gid_pn_resp { 72dbf5dfe9SChristof Schmitt struct fc_ct_hdr ct_hdr; 73dbf5dfe9SChristof Schmitt struct fc_gid_pn_resp gid_pn; 74dbf5dfe9SChristof Schmitt } __packed; 75dbf5dfe9SChristof Schmitt 76dbf5dfe9SChristof Schmitt /** 77dbf5dfe9SChristof Schmitt * struct zfcp_fc_gid_pn - everything required in zfcp for gid_pn request 78dbf5dfe9SChristof Schmitt * @ct: data passed to zfcp_fsf for issuing fsf request 79dbf5dfe9SChristof Schmitt * @sg_req: scatterlist entry for request data 80dbf5dfe9SChristof Schmitt * @sg_resp: scatterlist entry for response data 81dbf5dfe9SChristof Schmitt * @gid_pn_req: GID_PN request data 82dbf5dfe9SChristof Schmitt * @gid_pn_resp: GID_PN response data 83dbf5dfe9SChristof Schmitt */ 84dbf5dfe9SChristof Schmitt struct zfcp_fc_gid_pn { 857c7dc196SChristof Schmitt struct zfcp_fsf_ct_els ct; 86dbf5dfe9SChristof Schmitt struct scatterlist sg_req; 87dbf5dfe9SChristof Schmitt struct scatterlist sg_resp; 88dbf5dfe9SChristof Schmitt struct zfcp_fc_gid_pn_req gid_pn_req; 89dbf5dfe9SChristof Schmitt struct zfcp_fc_gid_pn_resp gid_pn_resp; 90dbf5dfe9SChristof Schmitt struct zfcp_port *port; 91dbf5dfe9SChristof Schmitt }; 92dbf5dfe9SChristof Schmitt 93dbf5dfe9SChristof Schmitt /** 94dbf5dfe9SChristof Schmitt * struct zfcp_fc_gpn_ft - container for ct header plus gpn_ft request 95dbf5dfe9SChristof Schmitt * @ct_hdr: FC GS common transport header 96dbf5dfe9SChristof Schmitt * @gpn_ft: GPN_FT request 97dbf5dfe9SChristof Schmitt */ 98dbf5dfe9SChristof Schmitt struct zfcp_fc_gpn_ft_req { 99dbf5dfe9SChristof Schmitt struct fc_ct_hdr ct_hdr; 100dbf5dfe9SChristof Schmitt struct fc_ns_gid_ft gpn_ft; 101dbf5dfe9SChristof Schmitt } __packed; 102dbf5dfe9SChristof Schmitt 103dbf5dfe9SChristof Schmitt /** 104dbf5dfe9SChristof Schmitt * struct zfcp_fc_gpn_ft_resp - container for ct header plus gpn_ft response 105dbf5dfe9SChristof Schmitt * @ct_hdr: FC GS common transport header 106dbf5dfe9SChristof Schmitt * @gpn_ft: Array of gpn_ft response data to fill one memory page 107dbf5dfe9SChristof Schmitt */ 108dbf5dfe9SChristof Schmitt struct zfcp_fc_gpn_ft_resp { 109dbf5dfe9SChristof Schmitt struct fc_ct_hdr ct_hdr; 110dbf5dfe9SChristof Schmitt struct fc_gpn_ft_resp gpn_ft[ZFCP_FC_GPN_FT_ENT_PAGE]; 111dbf5dfe9SChristof Schmitt } __packed; 112dbf5dfe9SChristof Schmitt 113dbf5dfe9SChristof Schmitt /** 114dbf5dfe9SChristof Schmitt * struct zfcp_fc_gpn_ft - zfcp data for gpn_ft request 115dbf5dfe9SChristof Schmitt * @ct: data passed to zfcp_fsf for issuing fsf request 116dbf5dfe9SChristof Schmitt * @sg_req: scatter list entry for gpn_ft request 117dbf5dfe9SChristof Schmitt * @sg_resp: scatter list entries for gpn_ft responses (per memory page) 118dbf5dfe9SChristof Schmitt */ 119dbf5dfe9SChristof Schmitt struct zfcp_fc_gpn_ft { 1207c7dc196SChristof Schmitt struct zfcp_fsf_ct_els ct; 121dbf5dfe9SChristof Schmitt struct scatterlist sg_req; 122dbf5dfe9SChristof Schmitt struct scatterlist sg_resp[ZFCP_FC_GPN_FT_NUM_BUFS]; 123dbf5dfe9SChristof Schmitt }; 124dbf5dfe9SChristof Schmitt 1254318e08cSChristof Schmitt /** 126*087897e3SChristof Schmitt * struct zfcp_fc_req - Container for FC ELS and CT requests sent from zfcp 127*087897e3SChristof Schmitt * @ct_els: data required for issuing fsf command 128*087897e3SChristof Schmitt * @sg_req: scatterlist entry for request data 129*087897e3SChristof Schmitt * @sg_rsp: scatterlist entry for response data 130*087897e3SChristof Schmitt * @u: request specific data 1319d05ce2cSChristof Schmitt */ 132*087897e3SChristof Schmitt struct zfcp_fc_req { 133*087897e3SChristof Schmitt struct zfcp_fsf_ct_els ct_els; 134*087897e3SChristof Schmitt struct scatterlist sg_req; 135*087897e3SChristof Schmitt struct scatterlist sg_rsp; 136*087897e3SChristof Schmitt union { 137*087897e3SChristof Schmitt struct { 138*087897e3SChristof Schmitt struct fc_els_adisc req; 139*087897e3SChristof Schmitt struct fc_els_adisc rsp; 140*087897e3SChristof Schmitt } adisc; 141*087897e3SChristof Schmitt } u; 1429d05ce2cSChristof Schmitt }; 1439d05ce2cSChristof Schmitt 1449d05ce2cSChristof Schmitt /** 145bd0072ecSChristof Schmitt * enum zfcp_fc_wka_status - FC WKA port status in zfcp 146bd0072ecSChristof Schmitt * @ZFCP_FC_WKA_PORT_OFFLINE: Port is closed and not in use 147bd0072ecSChristof Schmitt * @ZFCP_FC_WKA_PORT_CLOSING: The FSF "close port" request is pending 148bd0072ecSChristof Schmitt * @ZFCP_FC_WKA_PORT_OPENING: The FSF "open port" request is pending 149bd0072ecSChristof Schmitt * @ZFCP_FC_WKA_PORT_ONLINE: The port is open and the port handle is valid 150bd0072ecSChristof Schmitt */ 151bd0072ecSChristof Schmitt enum zfcp_fc_wka_status { 152bd0072ecSChristof Schmitt ZFCP_FC_WKA_PORT_OFFLINE, 153bd0072ecSChristof Schmitt ZFCP_FC_WKA_PORT_CLOSING, 154bd0072ecSChristof Schmitt ZFCP_FC_WKA_PORT_OPENING, 155bd0072ecSChristof Schmitt ZFCP_FC_WKA_PORT_ONLINE, 156bd0072ecSChristof Schmitt }; 157bd0072ecSChristof Schmitt 158bd0072ecSChristof Schmitt /** 159bd0072ecSChristof Schmitt * struct zfcp_fc_wka_port - representation of well-known-address (WKA) FC port 160bd0072ecSChristof Schmitt * @adapter: Pointer to adapter structure this WKA port belongs to 161bd0072ecSChristof Schmitt * @completion_wq: Wait for completion of open/close command 162bd0072ecSChristof Schmitt * @status: Current status of WKA port 163bd0072ecSChristof Schmitt * @refcount: Reference count to keep port open as long as it is in use 164bd0072ecSChristof Schmitt * @d_id: FC destination id or well-known-address 165bd0072ecSChristof Schmitt * @handle: FSF handle for the open WKA port 166bd0072ecSChristof Schmitt * @mutex: Mutex used during opening/closing state changes 167bd0072ecSChristof Schmitt * @work: For delaying the closing of the WKA port 168bd0072ecSChristof Schmitt */ 169bd0072ecSChristof Schmitt struct zfcp_fc_wka_port { 170bd0072ecSChristof Schmitt struct zfcp_adapter *adapter; 171bd0072ecSChristof Schmitt wait_queue_head_t completion_wq; 172bd0072ecSChristof Schmitt enum zfcp_fc_wka_status status; 173bd0072ecSChristof Schmitt atomic_t refcount; 174bd0072ecSChristof Schmitt u32 d_id; 175bd0072ecSChristof Schmitt u32 handle; 176bd0072ecSChristof Schmitt struct mutex mutex; 177bd0072ecSChristof Schmitt struct delayed_work work; 178bd0072ecSChristof Schmitt }; 179bd0072ecSChristof Schmitt 180bd0072ecSChristof Schmitt /** 181bd0072ecSChristof Schmitt * struct zfcp_fc_wka_ports - Data structures for FC generic services 182bd0072ecSChristof Schmitt * @ms: FC Management service 183bd0072ecSChristof Schmitt * @ts: FC time service 184bd0072ecSChristof Schmitt * @ds: FC directory service 185bd0072ecSChristof Schmitt * @as: FC alias service 186bd0072ecSChristof Schmitt */ 187bd0072ecSChristof Schmitt struct zfcp_fc_wka_ports { 188bd0072ecSChristof Schmitt struct zfcp_fc_wka_port ms; 189bd0072ecSChristof Schmitt struct zfcp_fc_wka_port ts; 190bd0072ecSChristof Schmitt struct zfcp_fc_wka_port ds; 191bd0072ecSChristof Schmitt struct zfcp_fc_wka_port as; 192bd0072ecSChristof Schmitt }; 193bd0072ecSChristof Schmitt 194bd0072ecSChristof Schmitt /** 1954318e08cSChristof Schmitt * zfcp_fc_scsi_to_fcp - setup FCP command with data from scsi_cmnd 1964318e08cSChristof Schmitt * @fcp: fcp_cmnd to setup 1974318e08cSChristof Schmitt * @scsi: scsi_cmnd where to get LUN, task attributes/flags and CDB 1984318e08cSChristof Schmitt */ 1994318e08cSChristof Schmitt static inline 2004318e08cSChristof Schmitt void zfcp_fc_scsi_to_fcp(struct fcp_cmnd *fcp, struct scsi_cmnd *scsi) 2014318e08cSChristof Schmitt { 2024318e08cSChristof Schmitt char tag[2]; 2034318e08cSChristof Schmitt 2044318e08cSChristof Schmitt int_to_scsilun(scsi->device->lun, (struct scsi_lun *) &fcp->fc_lun); 2054318e08cSChristof Schmitt 2064318e08cSChristof Schmitt if (scsi_populate_tag_msg(scsi, tag)) { 2074318e08cSChristof Schmitt switch (tag[0]) { 2084318e08cSChristof Schmitt case MSG_ORDERED_TAG: 2094318e08cSChristof Schmitt fcp->fc_pri_ta |= FCP_PTA_ORDERED; 2104318e08cSChristof Schmitt break; 2114318e08cSChristof Schmitt case MSG_SIMPLE_TAG: 2124318e08cSChristof Schmitt fcp->fc_pri_ta |= FCP_PTA_SIMPLE; 2134318e08cSChristof Schmitt break; 2144318e08cSChristof Schmitt }; 2154318e08cSChristof Schmitt } else 2164318e08cSChristof Schmitt fcp->fc_pri_ta = FCP_PTA_SIMPLE; 2174318e08cSChristof Schmitt 2184318e08cSChristof Schmitt if (scsi->sc_data_direction == DMA_FROM_DEVICE) 2194318e08cSChristof Schmitt fcp->fc_flags |= FCP_CFL_RDDATA; 2204318e08cSChristof Schmitt if (scsi->sc_data_direction == DMA_TO_DEVICE) 2214318e08cSChristof Schmitt fcp->fc_flags |= FCP_CFL_WRDATA; 2224318e08cSChristof Schmitt 2234318e08cSChristof Schmitt memcpy(fcp->fc_cdb, scsi->cmnd, scsi->cmd_len); 2244318e08cSChristof Schmitt 2254318e08cSChristof Schmitt fcp->fc_dl = scsi_bufflen(scsi); 226ef3eb71dSFelix Beck 227ef3eb71dSFelix Beck if (scsi_get_prot_type(scsi) == SCSI_PROT_DIF_TYPE1) 228ef3eb71dSFelix Beck fcp->fc_dl += fcp->fc_dl / scsi->device->sector_size * 8; 2294318e08cSChristof Schmitt } 2304318e08cSChristof Schmitt 2314318e08cSChristof Schmitt /** 2324318e08cSChristof Schmitt * zfcp_fc_fcp_tm - setup FCP command as task management command 2334318e08cSChristof Schmitt * @fcp: fcp_cmnd to setup 2344318e08cSChristof Schmitt * @dev: scsi_device where to send the task management command 2354318e08cSChristof Schmitt * @tm: task management flags to setup tm command 2364318e08cSChristof Schmitt */ 2374318e08cSChristof Schmitt static inline 2384318e08cSChristof Schmitt void zfcp_fc_fcp_tm(struct fcp_cmnd *fcp, struct scsi_device *dev, u8 tm_flags) 2394318e08cSChristof Schmitt { 2404318e08cSChristof Schmitt int_to_scsilun(dev->lun, (struct scsi_lun *) &fcp->fc_lun); 2414318e08cSChristof Schmitt fcp->fc_tm_flags |= tm_flags; 2424318e08cSChristof Schmitt } 2434318e08cSChristof Schmitt 2444318e08cSChristof Schmitt /** 2454318e08cSChristof Schmitt * zfcp_fc_evap_fcp_rsp - evaluate FCP RSP IU and update scsi_cmnd accordingly 2464318e08cSChristof Schmitt * @fcp_rsp: FCP RSP IU to evaluate 2474318e08cSChristof Schmitt * @scsi: SCSI command where to update status and sense buffer 2484318e08cSChristof Schmitt */ 2494318e08cSChristof Schmitt static inline 2504318e08cSChristof Schmitt void zfcp_fc_eval_fcp_rsp(struct fcp_resp_with_ext *fcp_rsp, 2514318e08cSChristof Schmitt struct scsi_cmnd *scsi) 2524318e08cSChristof Schmitt { 2534318e08cSChristof Schmitt struct fcp_resp_rsp_info *rsp_info; 2544318e08cSChristof Schmitt char *sense; 2554318e08cSChristof Schmitt u32 sense_len, resid; 2564318e08cSChristof Schmitt u8 rsp_flags; 2574318e08cSChristof Schmitt 2584318e08cSChristof Schmitt set_msg_byte(scsi, COMMAND_COMPLETE); 2594318e08cSChristof Schmitt scsi->result |= fcp_rsp->resp.fr_status; 2604318e08cSChristof Schmitt 2614318e08cSChristof Schmitt rsp_flags = fcp_rsp->resp.fr_flags; 2624318e08cSChristof Schmitt 2634318e08cSChristof Schmitt if (unlikely(rsp_flags & FCP_RSP_LEN_VAL)) { 2644318e08cSChristof Schmitt rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1]; 2654318e08cSChristof Schmitt if (rsp_info->rsp_code == FCP_TMF_CMPL) 2664318e08cSChristof Schmitt set_host_byte(scsi, DID_OK); 2674318e08cSChristof Schmitt else { 2684318e08cSChristof Schmitt set_host_byte(scsi, DID_ERROR); 2694318e08cSChristof Schmitt return; 2704318e08cSChristof Schmitt } 2714318e08cSChristof Schmitt } 2724318e08cSChristof Schmitt 2734318e08cSChristof Schmitt if (unlikely(rsp_flags & FCP_SNS_LEN_VAL)) { 2744318e08cSChristof Schmitt sense = (char *) &fcp_rsp[1]; 2754318e08cSChristof Schmitt if (rsp_flags & FCP_RSP_LEN_VAL) 276fb5a6389SChristof Schmitt sense += fcp_rsp->ext.fr_rsp_len; 2774318e08cSChristof Schmitt sense_len = min(fcp_rsp->ext.fr_sns_len, 2784318e08cSChristof Schmitt (u32) SCSI_SENSE_BUFFERSIZE); 2794318e08cSChristof Schmitt memcpy(scsi->sense_buffer, sense, sense_len); 2804318e08cSChristof Schmitt } 2814318e08cSChristof Schmitt 2824318e08cSChristof Schmitt if (unlikely(rsp_flags & FCP_RESID_UNDER)) { 2834318e08cSChristof Schmitt resid = fcp_rsp->ext.fr_resid; 2844318e08cSChristof Schmitt scsi_set_resid(scsi, resid); 2854318e08cSChristof Schmitt if (scsi_bufflen(scsi) - resid < scsi->underflow && 2864318e08cSChristof Schmitt !(rsp_flags & FCP_SNS_LEN_VAL) && 2874318e08cSChristof Schmitt fcp_rsp->resp.fr_status == SAM_STAT_GOOD) 2884318e08cSChristof Schmitt set_host_byte(scsi, DID_ERROR); 2894318e08cSChristof Schmitt } 2904318e08cSChristof Schmitt } 2914318e08cSChristof Schmitt 2924318e08cSChristof Schmitt #endif 293