blob: 4351b4e81b3cf38dfa33b73fff793b1403217e30 [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/**
67 * struct zfcp_fc_gid_pn_resp - container for ct header plus gid_pn response
68 * @ct_hdr: FC GS common transport header
69 * @gid_pn: GID_PN response
70 */
71struct zfcp_fc_gid_pn_resp {
72 struct fc_ct_hdr ct_hdr;
73 struct fc_gid_pn_resp gid_pn;
74} __packed;
75
76/**
77 * struct zfcp_fc_gid_pn - everything required in zfcp for gid_pn request
78 * @ct: data passed to zfcp_fsf for issuing fsf request
79 * @sg_req: scatterlist entry for request data
80 * @sg_resp: scatterlist entry for response data
81 * @gid_pn_req: GID_PN request data
82 * @gid_pn_resp: GID_PN response data
83 */
84struct zfcp_fc_gid_pn {
Christof Schmitt7c7dc192009-11-24 16:54:13 +010085 struct zfcp_fsf_ct_els ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +010086 struct scatterlist sg_req;
87 struct scatterlist sg_resp;
88 struct zfcp_fc_gid_pn_req gid_pn_req;
89 struct zfcp_fc_gid_pn_resp gid_pn_resp;
90 struct zfcp_port *port;
91};
92
93/**
94 * struct zfcp_fc_gpn_ft - container for ct header plus gpn_ft request
95 * @ct_hdr: FC GS common transport header
96 * @gpn_ft: GPN_FT request
97 */
98struct zfcp_fc_gpn_ft_req {
99 struct fc_ct_hdr ct_hdr;
100 struct fc_ns_gid_ft gpn_ft;
101} __packed;
102
103/**
104 * struct zfcp_fc_gpn_ft_resp - container for ct header plus gpn_ft response
105 * @ct_hdr: FC GS common transport header
106 * @gpn_ft: Array of gpn_ft response data to fill one memory page
107 */
108struct zfcp_fc_gpn_ft_resp {
109 struct fc_ct_hdr ct_hdr;
110 struct fc_gpn_ft_resp gpn_ft[ZFCP_FC_GPN_FT_ENT_PAGE];
111} __packed;
112
113/**
114 * struct zfcp_fc_gpn_ft - zfcp data for gpn_ft request
115 * @ct: data passed to zfcp_fsf for issuing fsf request
116 * @sg_req: scatter list entry for gpn_ft request
117 * @sg_resp: scatter list entries for gpn_ft responses (per memory page)
118 */
119struct zfcp_fc_gpn_ft {
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100120 struct zfcp_fsf_ct_els ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100121 struct scatterlist sg_req;
122 struct scatterlist sg_resp[ZFCP_FC_GPN_FT_NUM_BUFS];
123};
124
Christof Schmitt4318e082009-11-24 16:54:08 +0100125/**
Christof Schmitt087897e2011-02-22 19:54:41 +0100126 * struct zfcp_fc_req - Container for FC ELS and CT requests sent from zfcp
127 * @ct_els: data required for issuing fsf command
128 * @sg_req: scatterlist entry for request data
129 * @sg_rsp: scatterlist entry for response data
130 * @u: request specific data
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100131 */
Christof Schmitt087897e2011-02-22 19:54:41 +0100132struct zfcp_fc_req {
133 struct zfcp_fsf_ct_els ct_els;
134 struct scatterlist sg_req;
135 struct scatterlist sg_rsp;
136 union {
137 struct {
138 struct fc_els_adisc req;
139 struct fc_els_adisc rsp;
140 } adisc;
141 } u;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100142};
143
144/**
Christof Schmittbd0072e2009-11-24 16:54:11 +0100145 * enum zfcp_fc_wka_status - FC WKA port status in zfcp
146 * @ZFCP_FC_WKA_PORT_OFFLINE: Port is closed and not in use
147 * @ZFCP_FC_WKA_PORT_CLOSING: The FSF "close port" request is pending
148 * @ZFCP_FC_WKA_PORT_OPENING: The FSF "open port" request is pending
149 * @ZFCP_FC_WKA_PORT_ONLINE: The port is open and the port handle is valid
150 */
151enum zfcp_fc_wka_status {
152 ZFCP_FC_WKA_PORT_OFFLINE,
153 ZFCP_FC_WKA_PORT_CLOSING,
154 ZFCP_FC_WKA_PORT_OPENING,
155 ZFCP_FC_WKA_PORT_ONLINE,
156};
157
158/**
159 * struct zfcp_fc_wka_port - representation of well-known-address (WKA) FC port
160 * @adapter: Pointer to adapter structure this WKA port belongs to
161 * @completion_wq: Wait for completion of open/close command
162 * @status: Current status of WKA port
163 * @refcount: Reference count to keep port open as long as it is in use
164 * @d_id: FC destination id or well-known-address
165 * @handle: FSF handle for the open WKA port
166 * @mutex: Mutex used during opening/closing state changes
167 * @work: For delaying the closing of the WKA port
168 */
169struct zfcp_fc_wka_port {
170 struct zfcp_adapter *adapter;
171 wait_queue_head_t completion_wq;
172 enum zfcp_fc_wka_status status;
173 atomic_t refcount;
174 u32 d_id;
175 u32 handle;
176 struct mutex mutex;
177 struct delayed_work work;
178};
179
180/**
181 * struct zfcp_fc_wka_ports - Data structures for FC generic services
182 * @ms: FC Management service
183 * @ts: FC time service
184 * @ds: FC directory service
185 * @as: FC alias service
186 */
187struct zfcp_fc_wka_ports {
188 struct zfcp_fc_wka_port ms;
189 struct zfcp_fc_wka_port ts;
190 struct zfcp_fc_wka_port ds;
191 struct zfcp_fc_wka_port as;
192};
193
194/**
Christof Schmitt4318e082009-11-24 16:54:08 +0100195 * zfcp_fc_scsi_to_fcp - setup FCP command with data from scsi_cmnd
196 * @fcp: fcp_cmnd to setup
197 * @scsi: scsi_cmnd where to get LUN, task attributes/flags and CDB
198 */
199static inline
200void zfcp_fc_scsi_to_fcp(struct fcp_cmnd *fcp, struct scsi_cmnd *scsi)
201{
202 char tag[2];
203
204 int_to_scsilun(scsi->device->lun, (struct scsi_lun *) &fcp->fc_lun);
205
206 if (scsi_populate_tag_msg(scsi, tag)) {
207 switch (tag[0]) {
208 case MSG_ORDERED_TAG:
209 fcp->fc_pri_ta |= FCP_PTA_ORDERED;
210 break;
211 case MSG_SIMPLE_TAG:
212 fcp->fc_pri_ta |= FCP_PTA_SIMPLE;
213 break;
214 };
215 } else
216 fcp->fc_pri_ta = FCP_PTA_SIMPLE;
217
218 if (scsi->sc_data_direction == DMA_FROM_DEVICE)
219 fcp->fc_flags |= FCP_CFL_RDDATA;
220 if (scsi->sc_data_direction == DMA_TO_DEVICE)
221 fcp->fc_flags |= FCP_CFL_WRDATA;
222
223 memcpy(fcp->fc_cdb, scsi->cmnd, scsi->cmd_len);
224
225 fcp->fc_dl = scsi_bufflen(scsi);
Felix Beckef3eb712010-07-16 15:37:42 +0200226
227 if (scsi_get_prot_type(scsi) == SCSI_PROT_DIF_TYPE1)
228 fcp->fc_dl += fcp->fc_dl / scsi->device->sector_size * 8;
Christof Schmitt4318e082009-11-24 16:54:08 +0100229}
230
231/**
232 * zfcp_fc_fcp_tm - setup FCP command as task management command
233 * @fcp: fcp_cmnd to setup
234 * @dev: scsi_device where to send the task management command
235 * @tm: task management flags to setup tm command
236 */
237static inline
238void zfcp_fc_fcp_tm(struct fcp_cmnd *fcp, struct scsi_device *dev, u8 tm_flags)
239{
240 int_to_scsilun(dev->lun, (struct scsi_lun *) &fcp->fc_lun);
241 fcp->fc_tm_flags |= tm_flags;
242}
243
244/**
245 * zfcp_fc_evap_fcp_rsp - evaluate FCP RSP IU and update scsi_cmnd accordingly
246 * @fcp_rsp: FCP RSP IU to evaluate
247 * @scsi: SCSI command where to update status and sense buffer
248 */
249static inline
250void zfcp_fc_eval_fcp_rsp(struct fcp_resp_with_ext *fcp_rsp,
251 struct scsi_cmnd *scsi)
252{
253 struct fcp_resp_rsp_info *rsp_info;
254 char *sense;
255 u32 sense_len, resid;
256 u8 rsp_flags;
257
258 set_msg_byte(scsi, COMMAND_COMPLETE);
259 scsi->result |= fcp_rsp->resp.fr_status;
260
261 rsp_flags = fcp_rsp->resp.fr_flags;
262
263 if (unlikely(rsp_flags & FCP_RSP_LEN_VAL)) {
264 rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
265 if (rsp_info->rsp_code == FCP_TMF_CMPL)
266 set_host_byte(scsi, DID_OK);
267 else {
268 set_host_byte(scsi, DID_ERROR);
269 return;
270 }
271 }
272
273 if (unlikely(rsp_flags & FCP_SNS_LEN_VAL)) {
274 sense = (char *) &fcp_rsp[1];
275 if (rsp_flags & FCP_RSP_LEN_VAL)
Christof Schmittfb5a6382010-10-05 17:12:55 +0200276 sense += fcp_rsp->ext.fr_rsp_len;
Christof Schmitt4318e082009-11-24 16:54:08 +0100277 sense_len = min(fcp_rsp->ext.fr_sns_len,
278 (u32) SCSI_SENSE_BUFFERSIZE);
279 memcpy(scsi->sense_buffer, sense, sense_len);
280 }
281
282 if (unlikely(rsp_flags & FCP_RESID_UNDER)) {
283 resid = fcp_rsp->ext.fr_resid;
284 scsi_set_resid(scsi, resid);
285 if (scsi_bufflen(scsi) - resid < scsi->underflow &&
286 !(rsp_flags & FCP_SNS_LEN_VAL) &&
287 fcp_rsp->resp.fr_status == SAM_STAT_GOOD)
288 set_host_byte(scsi, DID_ERROR);
289 }
290}
291
292#endif