blob: 2066f9d9ceb5fdddc228788ebf650acf25768305 [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/**
Christof Schmitt087897e2011-02-22 19:54:41 +010087 * struct zfcp_fc_req - Container for FC ELS and CT requests sent from zfcp
88 * @ct_els: data required for issuing fsf command
89 * @sg_req: scatterlist entry for request data
90 * @sg_rsp: scatterlist entry for response data
91 * @u: request specific data
Christof Schmitt9d05ce22009-11-24 16:54:09 +010092 */
Christof Schmitt087897e2011-02-22 19:54:41 +010093struct zfcp_fc_req {
94 struct zfcp_fsf_ct_els ct_els;
95 struct scatterlist sg_req;
96 struct scatterlist sg_rsp;
97 union {
98 struct {
99 struct fc_els_adisc req;
100 struct fc_els_adisc rsp;
101 } adisc;
Christof Schmittfcf7e612011-02-22 19:54:42 +0100102 struct {
103 struct zfcp_fc_gid_pn_req req;
104 struct zfcp_fc_gid_pn_rsp rsp;
105 } gid_pn;
Christof Schmittf9773222011-02-22 19:54:43 +0100106 struct {
107 struct scatterlist sg_rsp2[ZFCP_FC_GPN_FT_NUM_BUFS - 1];
108 struct zfcp_fc_gpn_ft_req req;
109 } gpn_ft;
Christof Schmitt087897e2011-02-22 19:54:41 +0100110 } u;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100111};
112
113/**
Christof Schmittbd0072e2009-11-24 16:54:11 +0100114 * enum zfcp_fc_wka_status - FC WKA port status in zfcp
115 * @ZFCP_FC_WKA_PORT_OFFLINE: Port is closed and not in use
116 * @ZFCP_FC_WKA_PORT_CLOSING: The FSF "close port" request is pending
117 * @ZFCP_FC_WKA_PORT_OPENING: The FSF "open port" request is pending
118 * @ZFCP_FC_WKA_PORT_ONLINE: The port is open and the port handle is valid
119 */
120enum zfcp_fc_wka_status {
121 ZFCP_FC_WKA_PORT_OFFLINE,
122 ZFCP_FC_WKA_PORT_CLOSING,
123 ZFCP_FC_WKA_PORT_OPENING,
124 ZFCP_FC_WKA_PORT_ONLINE,
125};
126
127/**
128 * struct zfcp_fc_wka_port - representation of well-known-address (WKA) FC port
129 * @adapter: Pointer to adapter structure this WKA port belongs to
130 * @completion_wq: Wait for completion of open/close command
131 * @status: Current status of WKA port
132 * @refcount: Reference count to keep port open as long as it is in use
133 * @d_id: FC destination id or well-known-address
134 * @handle: FSF handle for the open WKA port
135 * @mutex: Mutex used during opening/closing state changes
136 * @work: For delaying the closing of the WKA port
137 */
138struct zfcp_fc_wka_port {
139 struct zfcp_adapter *adapter;
140 wait_queue_head_t completion_wq;
141 enum zfcp_fc_wka_status status;
142 atomic_t refcount;
143 u32 d_id;
144 u32 handle;
145 struct mutex mutex;
146 struct delayed_work work;
147};
148
149/**
150 * struct zfcp_fc_wka_ports - Data structures for FC generic services
151 * @ms: FC Management service
152 * @ts: FC time service
153 * @ds: FC directory service
154 * @as: FC alias service
155 */
156struct zfcp_fc_wka_ports {
157 struct zfcp_fc_wka_port ms;
158 struct zfcp_fc_wka_port ts;
159 struct zfcp_fc_wka_port ds;
160 struct zfcp_fc_wka_port as;
161};
162
163/**
Christof Schmitt4318e082009-11-24 16:54:08 +0100164 * zfcp_fc_scsi_to_fcp - setup FCP command with data from scsi_cmnd
165 * @fcp: fcp_cmnd to setup
166 * @scsi: scsi_cmnd where to get LUN, task attributes/flags and CDB
167 */
168static inline
169void zfcp_fc_scsi_to_fcp(struct fcp_cmnd *fcp, struct scsi_cmnd *scsi)
170{
171 char tag[2];
172
173 int_to_scsilun(scsi->device->lun, (struct scsi_lun *) &fcp->fc_lun);
174
175 if (scsi_populate_tag_msg(scsi, tag)) {
176 switch (tag[0]) {
177 case MSG_ORDERED_TAG:
178 fcp->fc_pri_ta |= FCP_PTA_ORDERED;
179 break;
180 case MSG_SIMPLE_TAG:
181 fcp->fc_pri_ta |= FCP_PTA_SIMPLE;
182 break;
183 };
184 } else
185 fcp->fc_pri_ta = FCP_PTA_SIMPLE;
186
187 if (scsi->sc_data_direction == DMA_FROM_DEVICE)
188 fcp->fc_flags |= FCP_CFL_RDDATA;
189 if (scsi->sc_data_direction == DMA_TO_DEVICE)
190 fcp->fc_flags |= FCP_CFL_WRDATA;
191
192 memcpy(fcp->fc_cdb, scsi->cmnd, scsi->cmd_len);
193
194 fcp->fc_dl = scsi_bufflen(scsi);
Felix Beckef3eb712010-07-16 15:37:42 +0200195
196 if (scsi_get_prot_type(scsi) == SCSI_PROT_DIF_TYPE1)
197 fcp->fc_dl += fcp->fc_dl / scsi->device->sector_size * 8;
Christof Schmitt4318e082009-11-24 16:54:08 +0100198}
199
200/**
201 * zfcp_fc_fcp_tm - setup FCP command as task management command
202 * @fcp: fcp_cmnd to setup
203 * @dev: scsi_device where to send the task management command
204 * @tm: task management flags to setup tm command
205 */
206static inline
207void zfcp_fc_fcp_tm(struct fcp_cmnd *fcp, struct scsi_device *dev, u8 tm_flags)
208{
209 int_to_scsilun(dev->lun, (struct scsi_lun *) &fcp->fc_lun);
210 fcp->fc_tm_flags |= tm_flags;
211}
212
213/**
214 * zfcp_fc_evap_fcp_rsp - evaluate FCP RSP IU and update scsi_cmnd accordingly
215 * @fcp_rsp: FCP RSP IU to evaluate
216 * @scsi: SCSI command where to update status and sense buffer
217 */
218static inline
219void zfcp_fc_eval_fcp_rsp(struct fcp_resp_with_ext *fcp_rsp,
220 struct scsi_cmnd *scsi)
221{
222 struct fcp_resp_rsp_info *rsp_info;
223 char *sense;
224 u32 sense_len, resid;
225 u8 rsp_flags;
226
227 set_msg_byte(scsi, COMMAND_COMPLETE);
228 scsi->result |= fcp_rsp->resp.fr_status;
229
230 rsp_flags = fcp_rsp->resp.fr_flags;
231
232 if (unlikely(rsp_flags & FCP_RSP_LEN_VAL)) {
233 rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
234 if (rsp_info->rsp_code == FCP_TMF_CMPL)
235 set_host_byte(scsi, DID_OK);
236 else {
237 set_host_byte(scsi, DID_ERROR);
238 return;
239 }
240 }
241
242 if (unlikely(rsp_flags & FCP_SNS_LEN_VAL)) {
243 sense = (char *) &fcp_rsp[1];
244 if (rsp_flags & FCP_RSP_LEN_VAL)
Christof Schmittfb5a6382010-10-05 17:12:55 +0200245 sense += fcp_rsp->ext.fr_rsp_len;
Christof Schmitt4318e082009-11-24 16:54:08 +0100246 sense_len = min(fcp_rsp->ext.fr_sns_len,
247 (u32) SCSI_SENSE_BUFFERSIZE);
248 memcpy(scsi->sense_buffer, sense, sense_len);
249 }
250
251 if (unlikely(rsp_flags & FCP_RESID_UNDER)) {
252 resid = fcp_rsp->ext.fr_resid;
253 scsi_set_resid(scsi, resid);
254 if (scsi_bufflen(scsi) - resid < scsi->underflow &&
255 !(rsp_flags & FCP_SNS_LEN_VAL) &&
256 fcp_rsp->resp.fr_status == SAM_STAT_GOOD)
257 set_host_byte(scsi, DID_ERROR);
258 }
259}
260
261#endif