blob: 200fe2501332a9a4d7039a330289da47f6a1e07c [file] [log] [blame]
Christof Schmitt4318e082009-11-24 16:54:08 +01001/*
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
Christof Schmitt9d05ce22009-11-24 16:54:09 +010013#include <scsi/fc/fc_els.h>
Christof Schmitt4318e082009-11-24 16:54:08 +010014#include <scsi/fc/fc_fcp.h>
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010015#include <scsi/fc/fc_ns.h>
Christof Schmitt4318e082009-11-24 16:54:08 +010016#include <scsi/scsi_cmnd.h>
17#include <scsi/scsi_tcq.h>
Christof Schmitt7c7dc192009-11-24 16:54:13 +010018#include "zfcp_fsf.h"
Christof Schmitt4318e082009-11-24 16:54:08 +010019
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010020#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
Swen Schillig51375ee2010-01-14 17:19:02 +010030#define ZFCP_FC_CTELS_TMO (2 * FC_DEF_R_A_TOV / 1000)
31
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010032/**
Sven Schuetz2d1e5472010-07-16 15:37:39 +020033 * struct zfcp_fc_event - FC HBAAPI event for internal queueing from irq context
34 * @code: Event code
35 * @data: Event data
36 * @list: list_head for zfcp_fc_events list
37 */
38struct zfcp_fc_event {
39 enum fc_host_event_code code;
40 u32 data;
41 struct list_head list;
42};
43
44/**
45 * struct zfcp_fc_events - Infrastructure for posting FC events from irq context
46 * @list: List for queueing of events from irq context to workqueue
47 * @list_lock: Lock for event list
48 * @work: work_struct for forwarding events in workqueue
49*/
50struct zfcp_fc_events {
51 struct list_head list;
52 spinlock_t list_lock;
53 struct work_struct work;
54};
55
56/**
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010057 * struct zfcp_fc_gid_pn_req - container for ct header plus gid_pn request
58 * @ct_hdr: FC GS common transport header
59 * @gid_pn: GID_PN request
60 */
61struct zfcp_fc_gid_pn_req {
62 struct fc_ct_hdr ct_hdr;
63 struct fc_ns_gid_pn gid_pn;
64} __packed;
65
66/**
Christof Schmittfcf7e612011-02-22 19:54:42 +010067 * struct zfcp_fc_gid_pn_rsp - container for ct header plus gid_pn response
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010068 * @ct_hdr: FC GS common transport header
69 * @gid_pn: GID_PN response
70 */
Christof Schmittfcf7e612011-02-22 19:54:42 +010071struct zfcp_fc_gid_pn_rsp {
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010072 struct fc_ct_hdr ct_hdr;
73 struct fc_gid_pn_resp gid_pn;
74} __packed;
75
76/**
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010077 * struct zfcp_fc_gpn_ft - container for ct header plus gpn_ft request
78 * @ct_hdr: FC GS common transport header
79 * @gpn_ft: GPN_FT request
80 */
81struct zfcp_fc_gpn_ft_req {
82 struct fc_ct_hdr ct_hdr;
83 struct fc_ns_gid_ft gpn_ft;
84} __packed;
85
86/**
87 * struct zfcp_fc_gpn_ft_resp - container for ct header plus gpn_ft response
88 * @ct_hdr: FC GS common transport header
89 * @gpn_ft: Array of gpn_ft response data to fill one memory page
90 */
91struct zfcp_fc_gpn_ft_resp {
92 struct fc_ct_hdr ct_hdr;
93 struct fc_gpn_ft_resp gpn_ft[ZFCP_FC_GPN_FT_ENT_PAGE];
94} __packed;
95
96/**
97 * struct zfcp_fc_gpn_ft - zfcp data for gpn_ft request
98 * @ct: data passed to zfcp_fsf for issuing fsf request
99 * @sg_req: scatter list entry for gpn_ft request
100 * @sg_resp: scatter list entries for gpn_ft responses (per memory page)
101 */
102struct zfcp_fc_gpn_ft {
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100103 struct zfcp_fsf_ct_els ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100104 struct scatterlist sg_req;
105 struct scatterlist sg_resp[ZFCP_FC_GPN_FT_NUM_BUFS];
106};
107
Christof Schmitt4318e082009-11-24 16:54:08 +0100108/**
Christof Schmitt087897e2011-02-22 19:54:41 +0100109 * struct zfcp_fc_req - Container for FC ELS and CT requests sent from zfcp
110 * @ct_els: data required for issuing fsf command
111 * @sg_req: scatterlist entry for request data
112 * @sg_rsp: scatterlist entry for response data
113 * @u: request specific data
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100114 */
Christof Schmitt087897e2011-02-22 19:54:41 +0100115struct zfcp_fc_req {
116 struct zfcp_fsf_ct_els ct_els;
117 struct scatterlist sg_req;
118 struct scatterlist sg_rsp;
119 union {
120 struct {
121 struct fc_els_adisc req;
122 struct fc_els_adisc rsp;
123 } adisc;
Christof Schmittfcf7e612011-02-22 19:54:42 +0100124 struct {
125 struct zfcp_fc_gid_pn_req req;
126 struct zfcp_fc_gid_pn_rsp rsp;
127 } gid_pn;
Christof Schmitt087897e2011-02-22 19:54:41 +0100128 } u;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100129};
130
131/**
Christof Schmittbd0072e2009-11-24 16:54:11 +0100132 * enum zfcp_fc_wka_status - FC WKA port status in zfcp
133 * @ZFCP_FC_WKA_PORT_OFFLINE: Port is closed and not in use
134 * @ZFCP_FC_WKA_PORT_CLOSING: The FSF "close port" request is pending
135 * @ZFCP_FC_WKA_PORT_OPENING: The FSF "open port" request is pending
136 * @ZFCP_FC_WKA_PORT_ONLINE: The port is open and the port handle is valid
137 */
138enum zfcp_fc_wka_status {
139 ZFCP_FC_WKA_PORT_OFFLINE,
140 ZFCP_FC_WKA_PORT_CLOSING,
141 ZFCP_FC_WKA_PORT_OPENING,
142 ZFCP_FC_WKA_PORT_ONLINE,
143};
144
145/**
146 * struct zfcp_fc_wka_port - representation of well-known-address (WKA) FC port
147 * @adapter: Pointer to adapter structure this WKA port belongs to
148 * @completion_wq: Wait for completion of open/close command
149 * @status: Current status of WKA port
150 * @refcount: Reference count to keep port open as long as it is in use
151 * @d_id: FC destination id or well-known-address
152 * @handle: FSF handle for the open WKA port
153 * @mutex: Mutex used during opening/closing state changes
154 * @work: For delaying the closing of the WKA port
155 */
156struct zfcp_fc_wka_port {
157 struct zfcp_adapter *adapter;
158 wait_queue_head_t completion_wq;
159 enum zfcp_fc_wka_status status;
160 atomic_t refcount;
161 u32 d_id;
162 u32 handle;
163 struct mutex mutex;
164 struct delayed_work work;
165};
166
167/**
168 * struct zfcp_fc_wka_ports - Data structures for FC generic services
169 * @ms: FC Management service
170 * @ts: FC time service
171 * @ds: FC directory service
172 * @as: FC alias service
173 */
174struct zfcp_fc_wka_ports {
175 struct zfcp_fc_wka_port ms;
176 struct zfcp_fc_wka_port ts;
177 struct zfcp_fc_wka_port ds;
178 struct zfcp_fc_wka_port as;
179};
180
181/**
Christof Schmitt4318e082009-11-24 16:54:08 +0100182 * zfcp_fc_scsi_to_fcp - setup FCP command with data from scsi_cmnd
183 * @fcp: fcp_cmnd to setup
184 * @scsi: scsi_cmnd where to get LUN, task attributes/flags and CDB
185 */
186static inline
187void zfcp_fc_scsi_to_fcp(struct fcp_cmnd *fcp, struct scsi_cmnd *scsi)
188{
189 char tag[2];
190
191 int_to_scsilun(scsi->device->lun, (struct scsi_lun *) &fcp->fc_lun);
192
193 if (scsi_populate_tag_msg(scsi, tag)) {
194 switch (tag[0]) {
195 case MSG_ORDERED_TAG:
196 fcp->fc_pri_ta |= FCP_PTA_ORDERED;
197 break;
198 case MSG_SIMPLE_TAG:
199 fcp->fc_pri_ta |= FCP_PTA_SIMPLE;
200 break;
201 };
202 } else
203 fcp->fc_pri_ta = FCP_PTA_SIMPLE;
204
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
210 memcpy(fcp->fc_cdb, scsi->cmnd, scsi->cmd_len);
211
212 fcp->fc_dl = scsi_bufflen(scsi);
Felix Beckef3eb712010-07-16 15:37:42 +0200213
214 if (scsi_get_prot_type(scsi) == SCSI_PROT_DIF_TYPE1)
215 fcp->fc_dl += fcp->fc_dl / scsi->device->sector_size * 8;
Christof Schmitt4318e082009-11-24 16:54:08 +0100216}
217
218/**
219 * zfcp_fc_fcp_tm - setup FCP command as task management command
220 * @fcp: fcp_cmnd to setup
221 * @dev: scsi_device where to send the task management command
222 * @tm: task management flags to setup tm command
223 */
224static inline
225void zfcp_fc_fcp_tm(struct fcp_cmnd *fcp, struct scsi_device *dev, u8 tm_flags)
226{
227 int_to_scsilun(dev->lun, (struct scsi_lun *) &fcp->fc_lun);
228 fcp->fc_tm_flags |= tm_flags;
229}
230
231/**
232 * zfcp_fc_evap_fcp_rsp - evaluate FCP RSP IU and update scsi_cmnd accordingly
233 * @fcp_rsp: FCP RSP IU to evaluate
234 * @scsi: SCSI command where to update status and sense buffer
235 */
236static inline
237void zfcp_fc_eval_fcp_rsp(struct fcp_resp_with_ext *fcp_rsp,
238 struct scsi_cmnd *scsi)
239{
240 struct fcp_resp_rsp_info *rsp_info;
241 char *sense;
242 u32 sense_len, resid;
243 u8 rsp_flags;
244
245 set_msg_byte(scsi, COMMAND_COMPLETE);
246 scsi->result |= fcp_rsp->resp.fr_status;
247
248 rsp_flags = fcp_rsp->resp.fr_flags;
249
250 if (unlikely(rsp_flags & FCP_RSP_LEN_VAL)) {
251 rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
252 if (rsp_info->rsp_code == FCP_TMF_CMPL)
253 set_host_byte(scsi, DID_OK);
254 else {
255 set_host_byte(scsi, DID_ERROR);
256 return;
257 }
258 }
259
260 if (unlikely(rsp_flags & FCP_SNS_LEN_VAL)) {
261 sense = (char *) &fcp_rsp[1];
262 if (rsp_flags & FCP_RSP_LEN_VAL)
Christof Schmittfb5a6382010-10-05 17:12:55 +0200263 sense += fcp_rsp->ext.fr_rsp_len;
Christof Schmitt4318e082009-11-24 16:54:08 +0100264 sense_len = min(fcp_rsp->ext.fr_sns_len,
265 (u32) SCSI_SENSE_BUFFERSIZE);
266 memcpy(scsi->sense_buffer, sense, sense_len);
267 }
268
269 if (unlikely(rsp_flags & FCP_RESID_UNDER)) {
270 resid = fcp_rsp->ext.fr_resid;
271 scsi_set_resid(scsi, resid);
272 if (scsi_bufflen(scsi) - resid < scsi->underflow &&
273 !(rsp_flags & FCP_SNS_LEN_VAL) &&
274 fcp_rsp->resp.fr_status == SAM_STAT_GOOD)
275 set_host_byte(scsi, DID_ERROR);
276 }
277}
278
279#endif