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