Merge branch 'for-2.6.37/drivers' of git://git.kernel.dk/linux-2.6-block
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / s390 / scsi / zfcp_fsf.c
CommitLineData
1da177e4 1/*
553448f6 2 * zfcp device driver
1da177e4 3 *
553448f6 4 * Implementation of FSF commands.
1da177e4 5 *
615f59e0 6 * Copyright IBM Corporation 2002, 2010
1da177e4
LT
7 */
8
ecf39d42
CS
9#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
0997f1c5 12#include <linux/blktrace_api.h>
5a0e3ad6 13#include <linux/slab.h>
9d05ce2c 14#include <scsi/fc/fc_els.h>
1da177e4 15#include "zfcp_ext.h"
4318e08c 16#include "zfcp_fc.h"
dcd20e23 17#include "zfcp_dbf.h"
34c2b712 18#include "zfcp_qdio.h"
b6bd2fb9 19#include "zfcp_reqlist.h"
1da177e4 20
287ac01a
CS
21static void zfcp_fsf_request_timeout_handler(unsigned long data)
22{
23 struct zfcp_adapter *adapter = (struct zfcp_adapter *) data;
339f4f4e 24 zfcp_qdio_siosl(adapter);
5ffd51a5
SS
25 zfcp_erp_adapter_reopen(adapter, ZFCP_STATUS_COMMON_ERP_FAILED,
26 "fsrth_1", NULL);
287ac01a
CS
27}
28
29static void zfcp_fsf_start_timer(struct zfcp_fsf_req *fsf_req,
30 unsigned long timeout)
31{
32 fsf_req->timer.function = zfcp_fsf_request_timeout_handler;
33 fsf_req->timer.data = (unsigned long) fsf_req->adapter;
34 fsf_req->timer.expires = jiffies + timeout;
35 add_timer(&fsf_req->timer);
36}
37
38static void zfcp_fsf_start_erp_timer(struct zfcp_fsf_req *fsf_req)
39{
40 BUG_ON(!fsf_req->erp_action);
41 fsf_req->timer.function = zfcp_erp_timeout_handler;
42 fsf_req->timer.data = (unsigned long) fsf_req->erp_action;
43 fsf_req->timer.expires = jiffies + 30 * HZ;
44 add_timer(&fsf_req->timer);
45}
46
1da177e4
LT
47/* association between FSF command and FSF QTCB type */
48static u32 fsf_qtcb_type[] = {
49 [FSF_QTCB_FCP_CMND] = FSF_IO_COMMAND,
50 [FSF_QTCB_ABORT_FCP_CMND] = FSF_SUPPORT_COMMAND,
51 [FSF_QTCB_OPEN_PORT_WITH_DID] = FSF_SUPPORT_COMMAND,
52 [FSF_QTCB_OPEN_LUN] = FSF_SUPPORT_COMMAND,
53 [FSF_QTCB_CLOSE_LUN] = FSF_SUPPORT_COMMAND,
54 [FSF_QTCB_CLOSE_PORT] = FSF_SUPPORT_COMMAND,
55 [FSF_QTCB_CLOSE_PHYSICAL_PORT] = FSF_SUPPORT_COMMAND,
56 [FSF_QTCB_SEND_ELS] = FSF_SUPPORT_COMMAND,
57 [FSF_QTCB_SEND_GENERIC] = FSF_SUPPORT_COMMAND,
58 [FSF_QTCB_EXCHANGE_CONFIG_DATA] = FSF_CONFIG_COMMAND,
59 [FSF_QTCB_EXCHANGE_PORT_DATA] = FSF_PORT_COMMAND,
60 [FSF_QTCB_DOWNLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND,
61 [FSF_QTCB_UPLOAD_CONTROL_FILE] = FSF_SUPPORT_COMMAND
62};
63
553448f6
CS
64static void zfcp_act_eval_err(struct zfcp_adapter *adapter, u32 table)
65{
c41f8cbd 66 u16 subtable = table >> 16;
553448f6 67 u16 rule = table & 0xffff;
ff3b24fa 68 const char *act_type[] = { "unknown", "OS", "WWPN", "DID", "LUN" };
553448f6 69
ff3b24fa 70 if (subtable && subtable < ARRAY_SIZE(act_type))
553448f6 71 dev_warn(&adapter->ccw_device->dev,
ff3b24fa
CS
72 "Access denied according to ACT rule type %s, "
73 "rule %d\n", act_type[subtable], rule);
553448f6
CS
74}
75
76static void zfcp_fsf_access_denied_port(struct zfcp_fsf_req *req,
77 struct zfcp_port *port)
78{
79 struct fsf_qtcb_header *header = &req->qtcb->header;
80 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 81 "Access denied to port 0x%016Lx\n",
7ba58c9c 82 (unsigned long long)port->wwpn);
553448f6
CS
83 zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
84 zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
5ffd51a5 85 zfcp_erp_port_access_denied(port, "fspad_1", req);
553448f6
CS
86 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
87}
88
89static void zfcp_fsf_access_denied_unit(struct zfcp_fsf_req *req,
90 struct zfcp_unit *unit)
91{
92 struct fsf_qtcb_header *header = &req->qtcb->header;
93 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 94 "Access denied to unit 0x%016Lx on port 0x%016Lx\n",
7ba58c9c
SS
95 (unsigned long long)unit->fcp_lun,
96 (unsigned long long)unit->port->wwpn);
553448f6
CS
97 zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[0]);
98 zfcp_act_eval_err(req->adapter, header->fsf_status_qual.halfword[1]);
5ffd51a5 99 zfcp_erp_unit_access_denied(unit, "fsuad_1", req);
553448f6
CS
100 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
101}
102
103static void zfcp_fsf_class_not_supp(struct zfcp_fsf_req *req)
104{
ff3b24fa
CS
105 dev_err(&req->adapter->ccw_device->dev, "FCP device not "
106 "operational because of an unsupported FC class\n");
5ffd51a5 107 zfcp_erp_adapter_shutdown(req->adapter, 0, "fscns_1", req);
553448f6
CS
108 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
109}
110
c41f8cbd
SS
111/**
112 * zfcp_fsf_req_free - free memory used by fsf request
113 * @fsf_req: pointer to struct zfcp_fsf_req
1da177e4 114 */
c41f8cbd 115void zfcp_fsf_req_free(struct zfcp_fsf_req *req)
1da177e4 116{
c41f8cbd 117 if (likely(req->pool)) {
a4623c46
SS
118 if (likely(req->qtcb))
119 mempool_free(req->qtcb, req->adapter->pool.qtcb_pool);
c41f8cbd 120 mempool_free(req, req->pool);
dd52e0ea
HC
121 return;
122 }
123
a4623c46
SS
124 if (likely(req->qtcb))
125 kmem_cache_free(zfcp_data.qtcb_cache, req->qtcb);
126 kfree(req);
1da177e4
LT
127}
128
c41f8cbd 129static void zfcp_fsf_status_read_port_closed(struct zfcp_fsf_req *req)
1da177e4 130{
ecf0c772 131 unsigned long flags;
c41f8cbd
SS
132 struct fsf_status_read_buffer *sr_buf = req->data;
133 struct zfcp_adapter *adapter = req->adapter;
134 struct zfcp_port *port;
800c0cad 135 int d_id = ntoh24(sr_buf->d_id);
1da177e4 136
ecf0c772
SS
137 read_lock_irqsave(&adapter->port_list_lock, flags);
138 list_for_each_entry(port, &adapter->port_list, list)
c41f8cbd 139 if (port->d_id == d_id) {
5ffd51a5 140 zfcp_erp_port_reopen(port, 0, "fssrpc1", req);
ecf0c772 141 break;
c41f8cbd 142 }
ecf0c772 143 read_unlock_irqrestore(&adapter->port_list_lock, flags);
1da177e4
LT
144}
145
5ffd51a5 146static void zfcp_fsf_link_down_info_eval(struct zfcp_fsf_req *req, char *id,
c41f8cbd 147 struct fsf_link_down_info *link_down)
aef4a983 148{
c41f8cbd 149 struct zfcp_adapter *adapter = req->adapter;
698ec016 150
c41f8cbd 151 if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
ee69ab7a
MS
152 return;
153
154 atomic_set_mask(ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED, &adapter->status);
70932935 155
a2fa0aed 156 zfcp_scsi_schedule_rports_block(adapter);
ee69ab7a 157
c41f8cbd 158 if (!link_down)
2f8f3ed5 159 goto out;
ee69ab7a 160
aef4a983
MS
161 switch (link_down->error_code) {
162 case FSF_PSQ_LINK_NO_LIGHT:
c41f8cbd 163 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
164 "There is no light signal from the local "
165 "fibre channel cable\n");
aef4a983
MS
166 break;
167 case FSF_PSQ_LINK_WRAP_PLUG:
c41f8cbd 168 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
169 "There is a wrap plug instead of a fibre "
170 "channel cable\n");
aef4a983
MS
171 break;
172 case FSF_PSQ_LINK_NO_FCP:
c41f8cbd 173 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
174 "The adjacent fibre channel node does not "
175 "support FCP\n");
aef4a983
MS
176 break;
177 case FSF_PSQ_LINK_FIRMWARE_UPDATE:
c41f8cbd 178 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
179 "The FCP device is suspended because of a "
180 "firmware update\n");
553448f6 181 break;
aef4a983 182 case FSF_PSQ_LINK_INVALID_WWPN:
c41f8cbd 183 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
184 "The FCP device detected a WWPN that is "
185 "duplicate or not valid\n");
aef4a983
MS
186 break;
187 case FSF_PSQ_LINK_NO_NPIV_SUPPORT:
c41f8cbd 188 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 189 "The fibre channel fabric does not support NPIV\n");
aef4a983
MS
190 break;
191 case FSF_PSQ_LINK_NO_FCP_RESOURCES:
c41f8cbd 192 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 193 "The FCP adapter cannot support more NPIV ports\n");
aef4a983
MS
194 break;
195 case FSF_PSQ_LINK_NO_FABRIC_RESOURCES:
c41f8cbd 196 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
197 "The adjacent switch cannot support "
198 "more NPIV ports\n");
aef4a983
MS
199 break;
200 case FSF_PSQ_LINK_FABRIC_LOGIN_UNABLE:
c41f8cbd 201 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
202 "The FCP adapter could not log in to the "
203 "fibre channel fabric\n");
aef4a983
MS
204 break;
205 case FSF_PSQ_LINK_WWPN_ASSIGNMENT_CORRUPTED:
c41f8cbd 206 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
207 "The WWPN assignment file on the FCP adapter "
208 "has been damaged\n");
aef4a983
MS
209 break;
210 case FSF_PSQ_LINK_MODE_TABLE_CURRUPTED:
c41f8cbd 211 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
212 "The mode table on the FCP adapter "
213 "has been damaged\n");
aef4a983
MS
214 break;
215 case FSF_PSQ_LINK_NO_WWPN_ASSIGNMENT:
c41f8cbd 216 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
217 "All NPIV ports on the FCP adapter have "
218 "been assigned\n");
aef4a983
MS
219 break;
220 default:
c41f8cbd 221 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa
CS
222 "The link between the FCP adapter and "
223 "the FC fabric is down\n");
aef4a983 224 }
c41f8cbd
SS
225out:
226 zfcp_erp_adapter_failed(adapter, id, req);
aef4a983
MS
227}
228
c41f8cbd 229static void zfcp_fsf_status_read_link_down(struct zfcp_fsf_req *req)
1da177e4 230{
c41f8cbd
SS
231 struct fsf_status_read_buffer *sr_buf = req->data;
232 struct fsf_link_down_info *ldi =
233 (struct fsf_link_down_info *) &sr_buf->payload;
1da177e4 234
c41f8cbd
SS
235 switch (sr_buf->status_subtype) {
236 case FSF_STATUS_READ_SUB_NO_PHYSICAL_LINK:
5ffd51a5 237 zfcp_fsf_link_down_info_eval(req, "fssrld1", ldi);
1da177e4 238 break;
c41f8cbd 239 case FSF_STATUS_READ_SUB_FDISC_FAILED:
5ffd51a5 240 zfcp_fsf_link_down_info_eval(req, "fssrld2", ldi);
1da177e4 241 break;
c41f8cbd 242 case FSF_STATUS_READ_SUB_FIRMWARE_UPDATE:
5ffd51a5 243 zfcp_fsf_link_down_info_eval(req, "fssrld3", NULL);
c41f8cbd
SS
244 };
245}
1da177e4 246
c41f8cbd
SS
247static void zfcp_fsf_status_read_handler(struct zfcp_fsf_req *req)
248{
249 struct zfcp_adapter *adapter = req->adapter;
250 struct fsf_status_read_buffer *sr_buf = req->data;
1da177e4 251
c41f8cbd 252 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
5771710b 253 zfcp_dbf_hba_fsf_unsol("dism", adapter->dbf, sr_buf);
a4623c46 254 mempool_free(sr_buf, adapter->pool.status_read_data);
c41f8cbd
SS
255 zfcp_fsf_req_free(req);
256 return;
257 }
1da177e4 258
5771710b 259 zfcp_dbf_hba_fsf_unsol("read", adapter->dbf, sr_buf);
1da177e4 260
c41f8cbd
SS
261 switch (sr_buf->status_type) {
262 case FSF_STATUS_READ_PORT_CLOSED:
263 zfcp_fsf_status_read_port_closed(req);
1da177e4 264 break;
c41f8cbd
SS
265 case FSF_STATUS_READ_INCOMING_ELS:
266 zfcp_fc_incoming_els(req);
1da177e4 267 break;
c41f8cbd 268 case FSF_STATUS_READ_SENSE_DATA_AVAIL:
1da177e4 269 break;
c41f8cbd 270 case FSF_STATUS_READ_BIT_ERROR_THRESHOLD:
ff3b24fa
CS
271 dev_warn(&adapter->ccw_device->dev,
272 "The error threshold for checksum statistics "
273 "has been exceeded\n");
5771710b 274 zfcp_dbf_hba_berr(adapter->dbf, req);
1da177e4 275 break;
c41f8cbd
SS
276 case FSF_STATUS_READ_LINK_DOWN:
277 zfcp_fsf_status_read_link_down(req);
2d1e547f 278 zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKDOWN, 0);
c41f8cbd
SS
279 break;
280 case FSF_STATUS_READ_LINK_UP:
281 dev_info(&adapter->ccw_device->dev,
ff3b24fa 282 "The local link has been restored\n");
c41f8cbd 283 /* All ports should be marked as ready to run again */
5ffd51a5 284 zfcp_erp_modify_adapter_status(adapter, "fssrh_1", NULL,
c41f8cbd
SS
285 ZFCP_STATUS_COMMON_RUNNING,
286 ZFCP_SET);
287 zfcp_erp_adapter_reopen(adapter,
288 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
289 ZFCP_STATUS_COMMON_ERP_FAILED,
5ffd51a5 290 "fssrh_2", req);
2d1e547f
SS
291 zfcp_fc_enqueue_event(adapter, FCH_EVT_LINKUP, 0);
292
c41f8cbd
SS
293 break;
294 case FSF_STATUS_READ_NOTIFICATION_LOST:
295 if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_ACT_UPDATED)
5ffd51a5
SS
296 zfcp_erp_adapter_access_changed(adapter, "fssrh_3",
297 req);
c41f8cbd 298 if (sr_buf->status_subtype & FSF_STATUS_READ_SUB_INCOMING_ELS)
9eae07ef 299 queue_work(adapter->work_queue, &adapter->scan_work);
c41f8cbd
SS
300 break;
301 case FSF_STATUS_READ_CFDC_UPDATED:
5ffd51a5 302 zfcp_erp_adapter_access_changed(adapter, "fssrh_4", req);
c41f8cbd
SS
303 break;
304 case FSF_STATUS_READ_FEATURE_UPDATE_ALERT:
305 adapter->adapter_features = sr_buf->payload.word[0];
1da177e4 306 break;
1da177e4
LT
307 }
308
a4623c46 309 mempool_free(sr_buf, adapter->pool.status_read_data);
c41f8cbd 310 zfcp_fsf_req_free(req);
1da177e4 311
c41f8cbd 312 atomic_inc(&adapter->stat_miss);
4544683a 313 queue_work(adapter->work_queue, &adapter->stat_work);
c41f8cbd 314}
1da177e4 315
c41f8cbd
SS
316static void zfcp_fsf_fsfstatus_qual_eval(struct zfcp_fsf_req *req)
317{
318 switch (req->qtcb->header.fsf_status_qual.word[0]) {
319 case FSF_SQ_FCP_RSP_AVAILABLE:
320 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
321 case FSF_SQ_NO_RETRY_POSSIBLE:
322 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
323 return;
324 case FSF_SQ_COMMAND_ABORTED:
c41f8cbd
SS
325 break;
326 case FSF_SQ_NO_RECOM:
327 dev_err(&req->adapter->ccw_device->dev,
ff3b24fa
CS
328 "The FCP adapter reported a problem "
329 "that cannot be recovered\n");
339f4f4e 330 zfcp_qdio_siosl(req->adapter);
5ffd51a5 331 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1", req);
c41f8cbd
SS
332 break;
333 }
334 /* all non-return stats set FSFREQ_ERROR*/
335 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
336}
337
c41f8cbd 338static void zfcp_fsf_fsfstatus_eval(struct zfcp_fsf_req *req)
1da177e4 339{
c41f8cbd
SS
340 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
341 return;
1da177e4 342
c41f8cbd
SS
343 switch (req->qtcb->header.fsf_status) {
344 case FSF_UNKNOWN_COMMAND:
345 dev_err(&req->adapter->ccw_device->dev,
ff3b24fa 346 "The FCP adapter does not recognize the command 0x%x\n",
c41f8cbd 347 req->qtcb->header.fsf_command);
5ffd51a5 348 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1", req);
c41f8cbd
SS
349 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
350 break;
351 case FSF_ADAPTER_STATUS_AVAILABLE:
352 zfcp_fsf_fsfstatus_qual_eval(req);
353 break;
354 }
355}
1da177e4 356
c41f8cbd
SS
357static void zfcp_fsf_protstatus_eval(struct zfcp_fsf_req *req)
358{
359 struct zfcp_adapter *adapter = req->adapter;
360 struct fsf_qtcb *qtcb = req->qtcb;
361 union fsf_prot_status_qual *psq = &qtcb->prefix.prot_status_qual;
1da177e4 362
5771710b 363 zfcp_dbf_hba_fsf_response(req);
1da177e4 364
c41f8cbd 365 if (req->status & ZFCP_STATUS_FSFREQ_DISMISSED) {
4c571c65 366 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
367 return;
368 }
1da177e4 369
c41f8cbd
SS
370 switch (qtcb->prefix.prot_status) {
371 case FSF_PROT_GOOD:
372 case FSF_PROT_FSF_STATUS_PRESENTED:
373 return;
374 case FSF_PROT_QTCB_VERSION_ERROR:
375 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
376 "QTCB version 0x%x not supported by FCP adapter "
377 "(0x%x to 0x%x)\n", FSF_QTCB_CURRENT_VERSION,
378 psq->word[0], psq->word[1]);
5ffd51a5 379 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1", req);
c41f8cbd
SS
380 break;
381 case FSF_PROT_ERROR_STATE:
382 case FSF_PROT_SEQ_NUMB_ERROR:
5ffd51a5 383 zfcp_erp_adapter_reopen(adapter, 0, "fspse_2", req);
4c571c65 384 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
385 break;
386 case FSF_PROT_UNSUPP_QTCB_TYPE:
387 dev_err(&adapter->ccw_device->dev,
ff3b24fa 388 "The QTCB type is not supported by the FCP adapter\n");
5ffd51a5 389 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3", req);
c41f8cbd
SS
390 break;
391 case FSF_PROT_HOST_CONNECTION_INITIALIZING:
392 atomic_set_mask(ZFCP_STATUS_ADAPTER_HOST_CON_INIT,
393 &adapter->status);
394 break;
395 case FSF_PROT_DUPLICATE_REQUEST_ID:
396 dev_err(&adapter->ccw_device->dev,
ff3b24fa 397 "0x%Lx is an ambiguous request identifier\n",
c41f8cbd 398 (unsigned long long)qtcb->bottom.support.req_handle);
5ffd51a5 399 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4", req);
c41f8cbd
SS
400 break;
401 case FSF_PROT_LINK_DOWN:
5ffd51a5
SS
402 zfcp_fsf_link_down_info_eval(req, "fspse_5",
403 &psq->link_down_info);
452b505c 404 /* go through reopen to flush pending requests */
5ffd51a5 405 zfcp_erp_adapter_reopen(adapter, 0, "fspse_6", req);
c41f8cbd
SS
406 break;
407 case FSF_PROT_REEST_QUEUE:
408 /* All ports should be marked as ready to run again */
5ffd51a5 409 zfcp_erp_modify_adapter_status(adapter, "fspse_7", NULL,
c41f8cbd
SS
410 ZFCP_STATUS_COMMON_RUNNING,
411 ZFCP_SET);
412 zfcp_erp_adapter_reopen(adapter,
413 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
5ffd51a5
SS
414 ZFCP_STATUS_COMMON_ERP_FAILED,
415 "fspse_8", req);
c41f8cbd
SS
416 break;
417 default:
418 dev_err(&adapter->ccw_device->dev,
ff3b24fa 419 "0x%x is not a valid transfer protocol status\n",
c41f8cbd 420 qtcb->prefix.prot_status);
339f4f4e 421 zfcp_qdio_siosl(adapter);
5ffd51a5 422 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9", req);
c41f8cbd
SS
423 }
424 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
425}
426
c41f8cbd
SS
427/**
428 * zfcp_fsf_req_complete - process completion of a FSF request
429 * @fsf_req: The FSF request that has been completed.
430 *
431 * When a request has been completed either from the FCP adapter,
432 * or it has been dismissed due to a queue shutdown, this function
433 * is called to process the completion status and trigger further
434 * events related to the FSF request.
435 */
bd63eaf4 436static void zfcp_fsf_req_complete(struct zfcp_fsf_req *req)
1da177e4 437{
c41f8cbd
SS
438 if (unlikely(req->fsf_command == FSF_QTCB_UNSOLICITED_STATUS)) {
439 zfcp_fsf_status_read_handler(req);
440 return;
441 }
1da177e4 442
c41f8cbd
SS
443 del_timer(&req->timer);
444 zfcp_fsf_protstatus_eval(req);
445 zfcp_fsf_fsfstatus_eval(req);
446 req->handler(req);
1da177e4 447
c41f8cbd 448 if (req->erp_action)
287ac01a 449 zfcp_erp_notify(req->erp_action, 0);
1da177e4 450
c41f8cbd
SS
451 if (likely(req->status & ZFCP_STATUS_FSFREQ_CLEANUP))
452 zfcp_fsf_req_free(req);
453 else
058b8647 454 complete(&req->completion);
c41f8cbd 455}
1da177e4 456
bd63eaf4
SS
457/**
458 * zfcp_fsf_req_dismiss_all - dismiss all fsf requests
459 * @adapter: pointer to struct zfcp_adapter
460 *
461 * Never ever call this without shutting down the adapter first.
462 * Otherwise the adapter would continue using and corrupting s390 storage.
463 * Included BUG_ON() call to ensure this is done.
464 * ERP is supposed to be the only user of this function.
465 */
466void zfcp_fsf_req_dismiss_all(struct zfcp_adapter *adapter)
467{
468 struct zfcp_fsf_req *req, *tmp;
bd63eaf4 469 LIST_HEAD(remove_queue);
bd63eaf4
SS
470
471 BUG_ON(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP);
b6bd2fb9 472 zfcp_reqlist_move(adapter->req_list, &remove_queue);
bd63eaf4
SS
473
474 list_for_each_entry_safe(req, tmp, &remove_queue, list) {
475 list_del(&req->list);
476 req->status |= ZFCP_STATUS_FSFREQ_DISMISSED;
477 zfcp_fsf_req_complete(req);
478 }
479}
480
c41f8cbd
SS
481static int zfcp_fsf_exchange_config_evaluate(struct zfcp_fsf_req *req)
482{
9d05ce2c 483 struct fsf_qtcb_bottom_config *bottom = &req->qtcb->bottom.config;
c41f8cbd
SS
484 struct zfcp_adapter *adapter = req->adapter;
485 struct Scsi_Host *shost = adapter->scsi_host;
9d05ce2c 486 struct fc_els_flogi *nsp, *plogi;
1da177e4 487
9d05ce2c
CS
488 /* adjust pointers for missing command code */
489 nsp = (struct fc_els_flogi *) ((u8 *)&bottom->nport_serv_param
490 - sizeof(u32));
491 plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload
492 - sizeof(u32));
1da177e4 493
c41f8cbd
SS
494 if (req->data)
495 memcpy(req->data, bottom, sizeof(*bottom));
496
9d05ce2c
CS
497 fc_host_port_name(shost) = nsp->fl_wwpn;
498 fc_host_node_name(shost) = nsp->fl_wwnn;
800c0cad 499 fc_host_port_id(shost) = ntoh24(bottom->s_id);
c41f8cbd
SS
500 fc_host_speed(shost) = bottom->fc_link_speed;
501 fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
502
503 adapter->hydra_version = bottom->adapter_type;
faf4cd85 504 adapter->timer_ticks = bottom->timer_interval & ZFCP_FSF_TIMER_INT_MASK;
8d88cf3f
CS
505 adapter->stat_read_buf_num = max(bottom->status_read_buf_num,
506 (u16)FSF_STATUS_READS_RECOM);
c41f8cbd
SS
507
508 if (fc_host_permanent_port_name(shost) == -1)
509 fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
510
511 switch (bottom->fc_topology) {
512 case FSF_TOPO_P2P:
800c0cad 513 adapter->peer_d_id = ntoh24(bottom->peer_d_id);
9d05ce2c
CS
514 adapter->peer_wwpn = plogi->fl_wwpn;
515 adapter->peer_wwnn = plogi->fl_wwnn;
c41f8cbd 516 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
c41f8cbd
SS
517 break;
518 case FSF_TOPO_FABRIC:
519 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
c41f8cbd
SS
520 break;
521 case FSF_TOPO_AL:
522 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
dceab655 523 /* fall through */
c41f8cbd 524 default:
c41f8cbd 525 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
526 "Unknown or unsupported arbitrated loop "
527 "fibre channel topology detected\n");
5ffd51a5 528 zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1", req);
c41f8cbd 529 return -EIO;
1da177e4 530 }
c41f8cbd 531
ef3eb71d
FB
532 zfcp_scsi_set_prot(adapter);
533
1da177e4
LT
534 return 0;
535}
536
c41f8cbd 537static void zfcp_fsf_exchange_config_data_handler(struct zfcp_fsf_req *req)
553448f6
CS
538{
539 struct zfcp_adapter *adapter = req->adapter;
c41f8cbd
SS
540 struct fsf_qtcb *qtcb = req->qtcb;
541 struct fsf_qtcb_bottom_config *bottom = &qtcb->bottom.config;
542 struct Scsi_Host *shost = adapter->scsi_host;
553448f6 543
c41f8cbd
SS
544 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
545 return;
1da177e4 546
c41f8cbd
SS
547 adapter->fsf_lic_version = bottom->lic_version;
548 adapter->adapter_features = bottom->adapter_features;
549 adapter->connection_features = bottom->connection_features;
550 adapter->peer_wwpn = 0;
551 adapter->peer_wwnn = 0;
552 adapter->peer_d_id = 0;
8a36e453 553
c41f8cbd
SS
554 switch (qtcb->header.fsf_status) {
555 case FSF_GOOD:
556 if (zfcp_fsf_exchange_config_evaluate(req))
557 return;
1da177e4 558
c41f8cbd
SS
559 if (bottom->max_qtcb_size < sizeof(struct fsf_qtcb)) {
560 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
561 "FCP adapter maximum QTCB size (%d bytes) "
562 "is too small\n",
563 bottom->max_qtcb_size);
5ffd51a5 564 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1", req);
c41f8cbd
SS
565 return;
566 }
567 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
568 &adapter->status);
1da177e4 569 break;
c41f8cbd
SS
570 case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
571 fc_host_node_name(shost) = 0;
572 fc_host_port_name(shost) = 0;
573 fc_host_port_id(shost) = 0;
574 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
575 fc_host_port_type(shost) = FC_PORTTYPE_UNKNOWN;
576 adapter->hydra_version = 0;
1da177e4 577
c41f8cbd
SS
578 atomic_set_mask(ZFCP_STATUS_ADAPTER_XCONFIG_OK,
579 &adapter->status);
1da177e4 580
5ffd51a5 581 zfcp_fsf_link_down_info_eval(req, "fsecdh2",
c41f8cbd 582 &qtcb->header.fsf_status_qual.link_down_info);
1da177e4 583 break;
c41f8cbd 584 default:
5ffd51a5 585 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3", req);
c41f8cbd
SS
586 return;
587 }
1da177e4 588
c41f8cbd
SS
589 if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
590 adapter->hardware_version = bottom->hardware_version;
591 memcpy(fc_host_serial_number(shost), bottom->serial_number,
592 min(FC_SERIAL_NUMBER_SIZE, 17));
593 EBCASC(fc_host_serial_number(shost),
594 min(FC_SERIAL_NUMBER_SIZE, 17));
595 }
1da177e4 596
c41f8cbd
SS
597 if (FSF_QTCB_CURRENT_VERSION < bottom->low_qtcb_version) {
598 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
599 "The FCP adapter only supports newer "
600 "control block versions\n");
5ffd51a5 601 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4", req);
c41f8cbd
SS
602 return;
603 }
604 if (FSF_QTCB_CURRENT_VERSION > bottom->high_qtcb_version) {
605 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
606 "The FCP adapter only supports older "
607 "control block versions\n");
5ffd51a5 608 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5", req);
c41f8cbd
SS
609 }
610}
1da177e4 611
c41f8cbd
SS
612static void zfcp_fsf_exchange_port_evaluate(struct zfcp_fsf_req *req)
613{
614 struct zfcp_adapter *adapter = req->adapter;
615 struct fsf_qtcb_bottom_port *bottom = &req->qtcb->bottom.port;
616 struct Scsi_Host *shost = adapter->scsi_host;
1da177e4 617
c41f8cbd
SS
618 if (req->data)
619 memcpy(req->data, bottom, sizeof(*bottom));
9eb69aff 620
0282985d 621 if (adapter->connection_features & FSF_FEATURE_NPIV_MODE) {
c41f8cbd 622 fc_host_permanent_port_name(shost) = bottom->wwpn;
0282985d
CS
623 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
624 } else
c41f8cbd
SS
625 fc_host_permanent_port_name(shost) = fc_host_port_name(shost);
626 fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
627 fc_host_supported_speeds(shost) = bottom->supported_speed;
2d8e62bb
CS
628 memcpy(fc_host_supported_fc4s(shost), bottom->supported_fc4_types,
629 FC_FC4_LIST_SIZE);
630 memcpy(fc_host_active_fc4s(shost), bottom->active_fc4_types,
631 FC_FC4_LIST_SIZE);
c41f8cbd 632}
1da177e4 633
c41f8cbd
SS
634static void zfcp_fsf_exchange_port_data_handler(struct zfcp_fsf_req *req)
635{
c41f8cbd
SS
636 struct fsf_qtcb *qtcb = req->qtcb;
637
638 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
639 return;
640
641 switch (qtcb->header.fsf_status) {
642 case FSF_GOOD:
643 zfcp_fsf_exchange_port_evaluate(req);
c41f8cbd
SS
644 break;
645 case FSF_EXCHANGE_CONFIG_DATA_INCOMPLETE:
646 zfcp_fsf_exchange_port_evaluate(req);
5ffd51a5 647 zfcp_fsf_link_down_info_eval(req, "fsepdh1",
c41f8cbd 648 &qtcb->header.fsf_status_qual.link_down_info);
aef4a983 649 break;
1da177e4 650 }
c41f8cbd 651}
d26ab06e 652
a4623c46 653static struct zfcp_fsf_req *zfcp_fsf_alloc(mempool_t *pool)
c41f8cbd
SS
654{
655 struct zfcp_fsf_req *req;
a4623c46
SS
656
657 if (likely(pool))
658 req = mempool_alloc(pool, GFP_ATOMIC);
659 else
660 req = kmalloc(sizeof(*req), GFP_ATOMIC);
661
662 if (unlikely(!req))
c41f8cbd 663 return NULL;
a4623c46 664
c41f8cbd 665 memset(req, 0, sizeof(*req));
88f2a977 666 req->pool = pool;
c41f8cbd
SS
667 return req;
668}
669
a4623c46 670static struct fsf_qtcb *zfcp_qtcb_alloc(mempool_t *pool)
c41f8cbd 671{
a4623c46 672 struct fsf_qtcb *qtcb;
c41f8cbd
SS
673
674 if (likely(pool))
675 qtcb = mempool_alloc(pool, GFP_ATOMIC);
676 else
a4623c46
SS
677 qtcb = kmem_cache_alloc(zfcp_data.qtcb_cache, GFP_ATOMIC);
678
c41f8cbd
SS
679 if (unlikely(!qtcb))
680 return NULL;
681
682 memset(qtcb, 0, sizeof(*qtcb));
a4623c46 683 return qtcb;
c41f8cbd
SS
684}
685
564e1c86 686static struct zfcp_fsf_req *zfcp_fsf_req_create(struct zfcp_qdio *qdio,
1674b405
CS
687 u32 fsf_cmd, u32 sbtype,
688 mempool_t *pool)
1da177e4 689{
564e1c86 690 struct zfcp_adapter *adapter = qdio->adapter;
a4623c46 691 struct zfcp_fsf_req *req = zfcp_fsf_alloc(pool);
1da177e4 692
c41f8cbd 693 if (unlikely(!req))
1e9b1643 694 return ERR_PTR(-ENOMEM);
1da177e4 695
c41f8cbd
SS
696 if (adapter->req_no == 0)
697 adapter->req_no++;
1da177e4 698
c41f8cbd
SS
699 INIT_LIST_HEAD(&req->list);
700 init_timer(&req->timer);
058b8647 701 init_completion(&req->completion);
1da177e4 702
c41f8cbd
SS
703 req->adapter = adapter;
704 req->fsf_command = fsf_cmd;
52bfb558 705 req->req_id = adapter->req_no;
c41f8cbd 706
a4623c46
SS
707 if (likely(fsf_cmd != FSF_QTCB_UNSOLICITED_STATUS)) {
708 if (likely(pool))
709 req->qtcb = zfcp_qtcb_alloc(adapter->pool.qtcb_pool);
710 else
711 req->qtcb = zfcp_qtcb_alloc(NULL);
712
713 if (unlikely(!req->qtcb)) {
714 zfcp_fsf_req_free(req);
715 return ERR_PTR(-ENOMEM);
716 }
717
5bdecd22 718 req->seq_no = adapter->fsf_req_seq_no;
564e1c86 719 req->qtcb->prefix.req_seq_no = adapter->fsf_req_seq_no;
c41f8cbd
SS
720 req->qtcb->prefix.req_id = req->req_id;
721 req->qtcb->prefix.ulp_info = 26;
722 req->qtcb->prefix.qtcb_type = fsf_qtcb_type[req->fsf_command];
723 req->qtcb->prefix.qtcb_version = FSF_QTCB_CURRENT_VERSION;
724 req->qtcb->header.req_handle = req->req_id;
725 req->qtcb->header.fsf_command = req->fsf_command;
c41f8cbd
SS
726 }
727
1674b405
CS
728 zfcp_qdio_req_init(adapter->qdio, &req->qdio_req, req->req_id, sbtype,
729 req->qtcb, sizeof(struct fsf_qtcb));
730
c41f8cbd 731 return req;
1da177e4
LT
732}
733
c41f8cbd
SS
734static int zfcp_fsf_req_send(struct zfcp_fsf_req *req)
735{
736 struct zfcp_adapter *adapter = req->adapter;
564e1c86 737 struct zfcp_qdio *qdio = adapter->qdio;
b6bd2fb9 738 int with_qtcb = (req->qtcb != NULL);
e60a6d69 739 int req_id = req->req_id;
c41f8cbd 740
b6bd2fb9 741 zfcp_reqlist_add(adapter->req_list, req);
c41f8cbd 742
706eca49 743 req->qdio_req.qdio_outb_usage = atomic_read(&qdio->req_q_free);
c41f8cbd 744 req->issued = get_clock();
34c2b712 745 if (zfcp_qdio_send(qdio, &req->qdio_req)) {
c41f8cbd 746 del_timer(&req->timer);
3765138a 747 /* lookup request again, list might have changed */
b6bd2fb9 748 zfcp_reqlist_find_rm(adapter->req_list, req_id);
5ffd51a5 749 zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1", req);
c41f8cbd
SS
750 return -EIO;
751 }
752
753 /* Don't increase for unsolicited status */
135ea137 754 if (with_qtcb)
c41f8cbd 755 adapter->fsf_req_seq_no++;
52bfb558 756 adapter->req_no++;
c41f8cbd
SS
757
758 return 0;
759}
760
761/**
762 * zfcp_fsf_status_read - send status read request
763 * @adapter: pointer to struct zfcp_adapter
764 * @req_flags: request flags
765 * Returns: 0 on success, ERROR otherwise
1da177e4 766 */
564e1c86 767int zfcp_fsf_status_read(struct zfcp_qdio *qdio)
1da177e4 768{
564e1c86 769 struct zfcp_adapter *adapter = qdio->adapter;
c41f8cbd
SS
770 struct zfcp_fsf_req *req;
771 struct fsf_status_read_buffer *sr_buf;
c41f8cbd 772 int retval = -EIO;
1da177e4 773
564e1c86 774 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 775 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
776 goto out;
777
1674b405 778 req = zfcp_fsf_req_create(qdio, FSF_QTCB_UNSOLICITED_STATUS, 0,
a4623c46 779 adapter->pool.status_read_req);
025270f0 780 if (IS_ERR(req)) {
c41f8cbd
SS
781 retval = PTR_ERR(req);
782 goto out;
1da177e4
LT
783 }
784
a4623c46 785 sr_buf = mempool_alloc(adapter->pool.status_read_data, GFP_ATOMIC);
c41f8cbd
SS
786 if (!sr_buf) {
787 retval = -ENOMEM;
788 goto failed_buf;
789 }
790 memset(sr_buf, 0, sizeof(*sr_buf));
791 req->data = sr_buf;
1674b405
CS
792
793 zfcp_qdio_fill_next(qdio, &req->qdio_req, sr_buf, sizeof(*sr_buf));
794 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
059c97d0 795
c41f8cbd
SS
796 retval = zfcp_fsf_req_send(req);
797 if (retval)
798 goto failed_req_send;
1da177e4 799
c41f8cbd
SS
800 goto out;
801
802failed_req_send:
a4623c46 803 mempool_free(sr_buf, adapter->pool.status_read_data);
c41f8cbd
SS
804failed_buf:
805 zfcp_fsf_req_free(req);
5771710b 806 zfcp_dbf_hba_fsf_unsol("fail", adapter->dbf, NULL);
c41f8cbd 807out:
564e1c86 808 spin_unlock_bh(&qdio->req_q_lock);
c41f8cbd
SS
809 return retval;
810}
811
812static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
813{
814 struct zfcp_unit *unit = req->data;
815 union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;
816
817 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
818 return;
819
820 switch (req->qtcb->header.fsf_status) {
1da177e4 821 case FSF_PORT_HANDLE_NOT_VALID:
c41f8cbd 822 if (fsq->word[0] == fsq->word[1]) {
5ffd51a5
SS
823 zfcp_erp_adapter_reopen(unit->port->adapter, 0,
824 "fsafch1", req);
c41f8cbd 825 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
826 }
827 break;
1da177e4 828 case FSF_LUN_HANDLE_NOT_VALID:
c41f8cbd 829 if (fsq->word[0] == fsq->word[1]) {
5ffd51a5 830 zfcp_erp_port_reopen(unit->port, 0, "fsafch2", req);
c41f8cbd 831 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
832 }
833 break;
1da177e4 834 case FSF_FCP_COMMAND_DOES_NOT_EXIST:
c41f8cbd 835 req->status |= ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED;
1da177e4 836 break;
1da177e4 837 case FSF_PORT_BOXED:
5ffd51a5 838 zfcp_erp_port_boxed(unit->port, "fsafch3", req);
4c571c65 839 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 840 break;
1da177e4 841 case FSF_LUN_BOXED:
5ffd51a5 842 zfcp_erp_unit_boxed(unit, "fsafch4", req);
4c571c65 843 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 844 break;
1da177e4 845 case FSF_ADAPTER_STATUS_AVAILABLE:
c41f8cbd 846 switch (fsq->word[0]) {
1da177e4 847 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
6f53a2d2 848 zfcp_fc_test_link(unit->port);
dceab655 849 /* fall through */
1da177e4 850 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 851 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 852 break;
1da177e4
LT
853 }
854 break;
1da177e4 855 case FSF_GOOD:
c41f8cbd 856 req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
1da177e4 857 break;
1da177e4 858 }
1da177e4
LT
859}
860
861/**
c41f8cbd
SS
862 * zfcp_fsf_abort_fcp_command - abort running SCSI command
863 * @old_req_id: unsigned long
c41f8cbd 864 * @unit: pointer to struct zfcp_unit
c41f8cbd 865 * Returns: pointer to struct zfcp_fsf_req
1da177e4 866 */
1da177e4 867
c41f8cbd 868struct zfcp_fsf_req *zfcp_fsf_abort_fcp_command(unsigned long old_req_id,
63caf367 869 struct zfcp_unit *unit)
1da177e4 870{
c41f8cbd 871 struct zfcp_fsf_req *req = NULL;
564e1c86 872 struct zfcp_qdio *qdio = unit->port->adapter->qdio;
8a36e453 873
564e1c86 874 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 875 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 876 goto out;
564e1c86 877 req = zfcp_fsf_req_create(qdio, FSF_QTCB_ABORT_FCP_CMND,
1674b405 878 SBAL_FLAGS0_TYPE_READ,
564e1c86 879 qdio->adapter->pool.scsi_abort);
633528c3
SS
880 if (IS_ERR(req)) {
881 req = NULL;
c41f8cbd 882 goto out;
633528c3 883 }
2abbe866 884
c41f8cbd
SS
885 if (unlikely(!(atomic_read(&unit->status) &
886 ZFCP_STATUS_COMMON_UNBLOCKED)))
887 goto out_error_free;
1da177e4 888
1674b405 889 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 890
c41f8cbd
SS
891 req->data = unit;
892 req->handler = zfcp_fsf_abort_fcp_command_handler;
893 req->qtcb->header.lun_handle = unit->handle;
894 req->qtcb->header.port_handle = unit->port->handle;
895 req->qtcb->bottom.support.req_handle = (u64) old_req_id;
896
897 zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
898 if (!zfcp_fsf_req_send(req))
899 goto out;
900
901out_error_free:
902 zfcp_fsf_req_free(req);
903 req = NULL;
904out:
564e1c86 905 spin_unlock_bh(&qdio->req_q_lock);
c41f8cbd 906 return req;
1da177e4
LT
907}
908
c41f8cbd 909static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
1da177e4 910{
c41f8cbd 911 struct zfcp_adapter *adapter = req->adapter;
7c7dc196 912 struct zfcp_fsf_ct_els *ct = req->data;
c41f8cbd 913 struct fsf_qtcb_header *header = &req->qtcb->header;
1da177e4 914
7c7dc196 915 ct->status = -EINVAL;
1da177e4 916
c41f8cbd 917 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1da177e4
LT
918 goto skip_fsfstatus;
919
1da177e4 920 switch (header->fsf_status) {
1da177e4 921 case FSF_GOOD:
5771710b 922 zfcp_dbf_san_ct_response(req);
7c7dc196 923 ct->status = 0;
1da177e4 924 break;
1da177e4 925 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
c41f8cbd 926 zfcp_fsf_class_not_supp(req);
1da177e4 927 break;
1da177e4 928 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
929 switch (header->fsf_status_qual.word[0]){
930 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 931 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 932 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 933 break;
1da177e4
LT
934 }
935 break;
1da177e4 936 case FSF_ACCESS_DENIED:
1da177e4 937 break;
1da177e4 938 case FSF_PORT_BOXED:
4c571c65 939 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 940 break;
c41f8cbd 941 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 942 zfcp_erp_adapter_reopen(adapter, 0, "fsscth1", req);
dceab655 943 /* fall through */
c41f8cbd 944 case FSF_GENERIC_COMMAND_REJECTED:
1da177e4 945 case FSF_PAYLOAD_SIZE_MISMATCH:
1da177e4 946 case FSF_REQUEST_SIZE_TOO_LARGE:
1da177e4 947 case FSF_RESPONSE_SIZE_TOO_LARGE:
1da177e4 948 case FSF_SBAL_MISMATCH:
c41f8cbd 949 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
950 break;
951 }
952
953skip_fsfstatus:
7c7dc196
CS
954 if (ct->handler)
955 ct->handler(ct->handler_data);
c41f8cbd 956}
1da177e4 957
1674b405
CS
958static void zfcp_fsf_setup_ct_els_unchained(struct zfcp_qdio *qdio,
959 struct zfcp_qdio_req *q_req,
426f6059
CS
960 struct scatterlist *sg_req,
961 struct scatterlist *sg_resp)
962{
1674b405
CS
963 zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_req), sg_req->length);
964 zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_resp), sg_resp->length);
965 zfcp_qdio_set_sbale_last(qdio, q_req);
426f6059
CS
966}
967
39eb7e9a
CS
968static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
969 struct scatterlist *sg_req,
01b04759 970 struct scatterlist *sg_resp)
c41f8cbd 971{
42428f74 972 struct zfcp_adapter *adapter = req->adapter;
42428f74 973 u32 feat = adapter->adapter_features;
c41f8cbd
SS
974 int bytes;
975
39eb7e9a 976 if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
1674b405
CS
977 if (!zfcp_qdio_sg_one_sbale(sg_req) ||
978 !zfcp_qdio_sg_one_sbale(sg_resp))
39eb7e9a
CS
979 return -EOPNOTSUPP;
980
1674b405
CS
981 zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
982 sg_req, sg_resp);
426f6059
CS
983 return 0;
984 }
985
986 /* use single, unchained SBAL if it can hold the request */
30b6777b 987 if (zfcp_qdio_sg_one_sbale(sg_req) && zfcp_qdio_sg_one_sbale(sg_resp)) {
1674b405
CS
988 zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
989 sg_req, sg_resp);
39eb7e9a
CS
990 return 0;
991 }
992
01b04759 993 bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->qdio_req, sg_req);
c41f8cbd 994 if (bytes <= 0)
9072df4d 995 return -EIO;
ef3eb71d 996 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
c41f8cbd 997 req->qtcb->bottom.support.req_buf_length = bytes;
1674b405 998 zfcp_qdio_skip_to_last_sbale(&req->qdio_req);
c41f8cbd 999
34c2b712 1000 bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->qdio_req,
01b04759 1001 sg_resp);
b1a58985 1002 req->qtcb->bottom.support.resp_buf_length = bytes;
c41f8cbd 1003 if (bytes <= 0)
9072df4d 1004 return -EIO;
ef3eb71d 1005 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
98fc4d5c 1006
b1a58985
CS
1007 return 0;
1008}
1009
1010static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
1011 struct scatterlist *sg_req,
1012 struct scatterlist *sg_resp,
01b04759 1013 unsigned int timeout)
b1a58985
CS
1014{
1015 int ret;
1016
01b04759 1017 ret = zfcp_fsf_setup_ct_els_sbals(req, sg_req, sg_resp);
b1a58985
CS
1018 if (ret)
1019 return ret;
1020
98fc4d5c 1021 /* common settings for ct/gs and els requests */
51375ee8
SS
1022 if (timeout > 255)
1023 timeout = 255; /* max value accepted by hardware */
98fc4d5c 1024 req->qtcb->bottom.support.service_class = FSF_CLASS_3;
51375ee8
SS
1025 req->qtcb->bottom.support.timeout = timeout;
1026 zfcp_fsf_start_timer(req, (timeout + 10) * HZ);
c41f8cbd
SS
1027
1028 return 0;
1da177e4
LT
1029}
1030
1031/**
c41f8cbd
SS
1032 * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
1033 * @ct: pointer to struct zfcp_send_ct with data for request
1034 * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
1da177e4 1035 */
7c7dc196 1036int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port,
51375ee8
SS
1037 struct zfcp_fsf_ct_els *ct, mempool_t *pool,
1038 unsigned int timeout)
1da177e4 1039{
564e1c86 1040 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
c41f8cbd
SS
1041 struct zfcp_fsf_req *req;
1042 int ret = -EIO;
1da177e4 1043
564e1c86 1044 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1045 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1046 goto out;
1da177e4 1047
1674b405
CS
1048 req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC,
1049 SBAL_FLAGS0_TYPE_WRITE_READ, pool);
09a46c6e 1050
025270f0 1051 if (IS_ERR(req)) {
c41f8cbd
SS
1052 ret = PTR_ERR(req);
1053 goto out;
3f0ca62a
CS
1054 }
1055
09a46c6e 1056 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
01b04759 1057 ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp, timeout);
553448f6 1058 if (ret)
1da177e4 1059 goto failed_send;
1da177e4 1060
c41f8cbd 1061 req->handler = zfcp_fsf_send_ct_handler;
5ab944f9 1062 req->qtcb->header.port_handle = wka_port->handle;
c41f8cbd
SS
1063 req->data = ct;
1064
7c7dc196 1065 zfcp_dbf_san_ct_request(req, wka_port->d_id);
1da177e4 1066
c41f8cbd
SS
1067 ret = zfcp_fsf_req_send(req);
1068 if (ret)
1069 goto failed_send;
1da177e4 1070
c41f8cbd 1071 goto out;
1da177e4 1072
c41f8cbd
SS
1073failed_send:
1074 zfcp_fsf_req_free(req);
c41f8cbd 1075out:
564e1c86 1076 spin_unlock_bh(&qdio->req_q_lock);
c41f8cbd 1077 return ret;
1da177e4
LT
1078}
1079
c41f8cbd 1080static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
1da177e4 1081{
7c7dc196 1082 struct zfcp_fsf_ct_els *send_els = req->data;
c41f8cbd
SS
1083 struct zfcp_port *port = send_els->port;
1084 struct fsf_qtcb_header *header = &req->qtcb->header;
1da177e4 1085
c41f8cbd 1086 send_els->status = -EINVAL;
1da177e4 1087
c41f8cbd 1088 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1da177e4
LT
1089 goto skip_fsfstatus;
1090
1091 switch (header->fsf_status) {
1da177e4 1092 case FSF_GOOD:
5771710b 1093 zfcp_dbf_san_els_response(req);
c41f8cbd 1094 send_els->status = 0;
1da177e4 1095 break;
1da177e4 1096 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
c41f8cbd 1097 zfcp_fsf_class_not_supp(req);
1da177e4 1098 break;
1da177e4 1099 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1100 switch (header->fsf_status_qual.word[0]){
1101 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 1102 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1da177e4 1103 case FSF_SQ_RETRY_IF_POSSIBLE:
c41f8cbd 1104 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1105 break;
1da177e4
LT
1106 }
1107 break;
1da177e4 1108 case FSF_ELS_COMMAND_REJECTED:
1da177e4 1109 case FSF_PAYLOAD_SIZE_MISMATCH:
1da177e4 1110 case FSF_REQUEST_SIZE_TOO_LARGE:
1da177e4 1111 case FSF_RESPONSE_SIZE_TOO_LARGE:
1da177e4 1112 break;
1da177e4 1113 case FSF_ACCESS_DENIED:
dc577d55
CS
1114 if (port)
1115 zfcp_fsf_access_denied_port(req, port);
1da177e4 1116 break;
c41f8cbd
SS
1117 case FSF_SBAL_MISMATCH:
1118 /* should never occure, avoided in zfcp_fsf_send_els */
1119 /* fall through */
1da177e4 1120 default:
c41f8cbd 1121 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1122 break;
1123 }
1da177e4 1124skip_fsfstatus:
aa551daf 1125 if (send_els->handler)
1da177e4 1126 send_els->handler(send_els->handler_data);
c41f8cbd 1127}
1da177e4 1128
c41f8cbd
SS
1129/**
1130 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1131 * @els: pointer to struct zfcp_send_els with data for the command
1132 */
7c7dc196 1133int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,
51375ee8 1134 struct zfcp_fsf_ct_els *els, unsigned int timeout)
c41f8cbd
SS
1135{
1136 struct zfcp_fsf_req *req;
7c7dc196 1137 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
1138 int ret = -EIO;
1139
564e1c86 1140 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1141 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1142 goto out;
09a46c6e 1143
1674b405
CS
1144 req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS,
1145 SBAL_FLAGS0_TYPE_WRITE_READ, NULL);
09a46c6e 1146
025270f0 1147 if (IS_ERR(req)) {
c41f8cbd
SS
1148 ret = PTR_ERR(req);
1149 goto out;
1150 }
1151
09a46c6e 1152 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
01b04759
SS
1153
1154 zfcp_qdio_sbal_limit(qdio, &req->qdio_req, 2);
1155
1156 ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, timeout);
44cc76f2 1157
c41f8cbd
SS
1158 if (ret)
1159 goto failed_send;
1160
7c7dc196 1161 hton24(req->qtcb->bottom.support.d_id, d_id);
c41f8cbd 1162 req->handler = zfcp_fsf_send_els_handler;
c41f8cbd
SS
1163 req->data = els;
1164
5771710b 1165 zfcp_dbf_san_els_request(req);
c41f8cbd 1166
c41f8cbd
SS
1167 ret = zfcp_fsf_req_send(req);
1168 if (ret)
1169 goto failed_send;
1170
1171 goto out;
1172
1173failed_send:
1174 zfcp_fsf_req_free(req);
1175out:
564e1c86 1176 spin_unlock_bh(&qdio->req_q_lock);
c41f8cbd 1177 return ret;
1da177e4
LT
1178}
1179
c41f8cbd 1180int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1da177e4 1181{
c41f8cbd 1182 struct zfcp_fsf_req *req;
564e1c86 1183 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1184 int retval = -EIO;
1185
564e1c86 1186 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1187 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1188 goto out;
09a46c6e 1189
564e1c86 1190 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1674b405 1191 SBAL_FLAGS0_TYPE_READ,
564e1c86 1192 qdio->adapter->pool.erp_req);
09a46c6e 1193
025270f0 1194 if (IS_ERR(req)) {
c41f8cbd
SS
1195 retval = PTR_ERR(req);
1196 goto out;
1da177e4
LT
1197 }
1198
09a46c6e 1199 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1200 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1201
c41f8cbd 1202 req->qtcb->bottom.config.feature_selection =
aef4a983
MS
1203 FSF_FEATURE_CFDC |
1204 FSF_FEATURE_LUN_SHARING |
9eb69aff 1205 FSF_FEATURE_NOTIFICATION_LOST |
aef4a983 1206 FSF_FEATURE_UPDATE_ALERT;
c41f8cbd
SS
1207 req->erp_action = erp_action;
1208 req->handler = zfcp_fsf_exchange_config_data_handler;
e60a6d69 1209 erp_action->fsf_req_id = req->req_id;
1da177e4 1210
287ac01a 1211 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1212 retval = zfcp_fsf_req_send(req);
1da177e4 1213 if (retval) {
c41f8cbd 1214 zfcp_fsf_req_free(req);
e60a6d69 1215 erp_action->fsf_req_id = 0;
1da177e4 1216 }
c41f8cbd 1217out:
564e1c86 1218 spin_unlock_bh(&qdio->req_q_lock);
52ef11a7
SS
1219 return retval;
1220}
1da177e4 1221
564e1c86 1222int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
c41f8cbd 1223 struct fsf_qtcb_bottom_config *data)
52ef11a7 1224{
c41f8cbd
SS
1225 struct zfcp_fsf_req *req = NULL;
1226 int retval = -EIO;
1227
564e1c86 1228 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1229 if (zfcp_qdio_sbal_get(qdio))
ada81b74 1230 goto out_unlock;
c41f8cbd 1231
1674b405
CS
1232 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1233 SBAL_FLAGS0_TYPE_READ, NULL);
09a46c6e 1234
025270f0 1235 if (IS_ERR(req)) {
c41f8cbd 1236 retval = PTR_ERR(req);
ada81b74 1237 goto out_unlock;
52ef11a7
SS
1238 }
1239
1674b405 1240 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
c41f8cbd 1241 req->handler = zfcp_fsf_exchange_config_data_handler;
52ef11a7 1242
c41f8cbd 1243 req->qtcb->bottom.config.feature_selection =
52ef11a7
SS
1244 FSF_FEATURE_CFDC |
1245 FSF_FEATURE_LUN_SHARING |
1246 FSF_FEATURE_NOTIFICATION_LOST |
1247 FSF_FEATURE_UPDATE_ALERT;
1248
1249 if (data)
c41f8cbd 1250 req->data = data;
52ef11a7 1251
c41f8cbd
SS
1252 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1253 retval = zfcp_fsf_req_send(req);
564e1c86 1254 spin_unlock_bh(&qdio->req_q_lock);
553448f6 1255 if (!retval)
058b8647 1256 wait_for_completion(&req->completion);
52ef11a7 1257
c41f8cbd 1258 zfcp_fsf_req_free(req);
ada81b74 1259 return retval;
52ef11a7 1260
ada81b74 1261out_unlock:
564e1c86 1262 spin_unlock_bh(&qdio->req_q_lock);
1da177e4
LT
1263 return retval;
1264}
1265
1da177e4
LT
1266/**
1267 * zfcp_fsf_exchange_port_data - request information about local port
aef4a983 1268 * @erp_action: ERP action for the adapter for which port data is requested
c41f8cbd 1269 * Returns: 0 on success, error otherwise
1da177e4 1270 */
c41f8cbd 1271int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
1da177e4 1272{
564e1c86 1273 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd 1274 struct zfcp_fsf_req *req;
c41f8cbd 1275 int retval = -EIO;
1da177e4 1276
564e1c86 1277 if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
52ef11a7 1278 return -EOPNOTSUPP;
1da177e4 1279
564e1c86 1280 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1281 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1282 goto out;
09a46c6e 1283
564e1c86 1284 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1674b405 1285 SBAL_FLAGS0_TYPE_READ,
564e1c86 1286 qdio->adapter->pool.erp_req);
09a46c6e 1287
025270f0 1288 if (IS_ERR(req)) {
c41f8cbd
SS
1289 retval = PTR_ERR(req);
1290 goto out;
aef4a983
MS
1291 }
1292
09a46c6e 1293 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1294 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1295
c41f8cbd
SS
1296 req->handler = zfcp_fsf_exchange_port_data_handler;
1297 req->erp_action = erp_action;
e60a6d69 1298 erp_action->fsf_req_id = req->req_id;
52ef11a7 1299
287ac01a 1300 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1301 retval = zfcp_fsf_req_send(req);
1da177e4 1302 if (retval) {
c41f8cbd 1303 zfcp_fsf_req_free(req);
e60a6d69 1304 erp_action->fsf_req_id = 0;
52ef11a7 1305 }
c41f8cbd 1306out:
564e1c86 1307 spin_unlock_bh(&qdio->req_q_lock);
52ef11a7
SS
1308 return retval;
1309}
1310
52ef11a7
SS
1311/**
1312 * zfcp_fsf_exchange_port_data_sync - request information about local port
564e1c86 1313 * @qdio: pointer to struct zfcp_qdio
c41f8cbd
SS
1314 * @data: pointer to struct fsf_qtcb_bottom_port
1315 * Returns: 0 on success, error otherwise
52ef11a7 1316 */
564e1c86 1317int zfcp_fsf_exchange_port_data_sync(struct zfcp_qdio *qdio,
c41f8cbd 1318 struct fsf_qtcb_bottom_port *data)
52ef11a7 1319{
c41f8cbd
SS
1320 struct zfcp_fsf_req *req = NULL;
1321 int retval = -EIO;
52ef11a7 1322
564e1c86 1323 if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
52ef11a7 1324 return -EOPNOTSUPP;
52ef11a7 1325
564e1c86 1326 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1327 if (zfcp_qdio_sbal_get(qdio))
ada81b74 1328 goto out_unlock;
c41f8cbd 1329
1674b405
CS
1330 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1331 SBAL_FLAGS0_TYPE_READ, NULL);
09a46c6e 1332
025270f0 1333 if (IS_ERR(req)) {
c41f8cbd 1334 retval = PTR_ERR(req);
ada81b74 1335 goto out_unlock;
1da177e4
LT
1336 }
1337
52ef11a7 1338 if (data)
c41f8cbd 1339 req->data = data;
52ef11a7 1340
1674b405 1341 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
52ef11a7 1342
c41f8cbd
SS
1343 req->handler = zfcp_fsf_exchange_port_data_handler;
1344 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1345 retval = zfcp_fsf_req_send(req);
564e1c86 1346 spin_unlock_bh(&qdio->req_q_lock);
ada81b74 1347
553448f6 1348 if (!retval)
058b8647
SS
1349 wait_for_completion(&req->completion);
1350
c41f8cbd 1351 zfcp_fsf_req_free(req);
1da177e4 1352
1da177e4 1353 return retval;
ada81b74
CS
1354
1355out_unlock:
564e1c86 1356 spin_unlock_bh(&qdio->req_q_lock);
ada81b74 1357 return retval;
1da177e4
LT
1358}
1359
c41f8cbd 1360static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
1da177e4 1361{
c41f8cbd
SS
1362 struct zfcp_port *port = req->data;
1363 struct fsf_qtcb_header *header = &req->qtcb->header;
9d05ce2c 1364 struct fc_els_flogi *plogi;
1da177e4 1365
c41f8cbd 1366 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
a17c5855 1367 goto out;
1da177e4 1368
1da177e4 1369 switch (header->fsf_status) {
1da177e4 1370 case FSF_PORT_ALREADY_OPEN:
1da177e4 1371 break;
1da177e4 1372 case FSF_ACCESS_DENIED:
c41f8cbd 1373 zfcp_fsf_access_denied_port(req, port);
1da177e4 1374 break;
1da177e4 1375 case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
c41f8cbd 1376 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 1377 "Not enough FCP adapter resources to open "
7ba58c9c
SS
1378 "remote port 0x%016Lx\n",
1379 (unsigned long long)port->wwpn);
5ffd51a5 1380 zfcp_erp_port_failed(port, "fsoph_1", req);
c41f8cbd 1381 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1382 break;
1da177e4 1383 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1384 switch (header->fsf_status_qual.word[0]) {
1385 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 1386 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1da177e4 1387 case FSF_SQ_NO_RETRY_POSSIBLE:
c41f8cbd 1388 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1389 break;
1390 }
1391 break;
1da177e4 1392 case FSF_GOOD:
1da177e4 1393 port->handle = header->port_handle;
1da177e4
LT
1394 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
1395 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
d736a27b
AH
1396 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1397 ZFCP_STATUS_COMMON_ACCESS_BOXED,
1398 &port->status);
1da177e4
LT
1399 /* check whether D_ID has changed during open */
1400 /*
1401 * FIXME: This check is not airtight, as the FCP channel does
1402 * not monitor closures of target port connections caused on
1403 * the remote side. Thus, they might miss out on invalidating
1404 * locally cached WWPNs (and other N_Port parameters) of gone
1405 * target ports. So, our heroic attempt to make things safe
1406 * could be undermined by 'open port' response data tagged with
1407 * obsolete WWPNs. Another reason to monitor potential
1408 * connection closures ourself at least (by interpreting
1409 * incoming ELS' and unsolicited status). It just crosses my
1410 * mind that one should be able to cross-check by means of
1411 * another GID_PN straight after a port has been opened.
1412 * Alternately, an ADISC/PDISC ELS should suffice, as well.
1413 */
9d05ce2c 1414 plogi = (struct fc_els_flogi *) req->qtcb->bottom.support.els;
39eb7e9a 1415 if (req->qtcb->bottom.support.els1_length >=
9d05ce2c 1416 FSF_PLOGI_MIN_LEN)
c41f8cbd 1417 zfcp_fc_plogi_evaluate(port, plogi);
1da177e4 1418 break;
1da177e4 1419 case FSF_UNKNOWN_OP_SUBTYPE:
c41f8cbd 1420 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1421 break;
1422 }
a17c5855
MP
1423
1424out:
615f59e0 1425 put_device(&port->dev);
1da177e4
LT
1426}
1427
c41f8cbd
SS
1428/**
1429 * zfcp_fsf_open_port - create and send open port request
1430 * @erp_action: pointer to struct zfcp_erp_action
1431 * Returns: 0 on success, error otherwise
1da177e4 1432 */
c41f8cbd 1433int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
1da177e4 1434{
564e1c86 1435 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
a17c5855 1436 struct zfcp_port *port = erp_action->port;
564e1c86 1437 struct zfcp_fsf_req *req;
c41f8cbd
SS
1438 int retval = -EIO;
1439
564e1c86 1440 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1441 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
1442 goto out;
1443
564e1c86 1444 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1674b405 1445 SBAL_FLAGS0_TYPE_READ,
564e1c86 1446 qdio->adapter->pool.erp_req);
09a46c6e 1447
025270f0 1448 if (IS_ERR(req)) {
c41f8cbd 1449 retval = PTR_ERR(req);
1da177e4 1450 goto out;
c41f8cbd 1451 }
1da177e4 1452
09a46c6e 1453 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1454 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1455
c41f8cbd 1456 req->handler = zfcp_fsf_open_port_handler;
800c0cad 1457 hton24(req->qtcb->bottom.support.d_id, port->d_id);
a17c5855 1458 req->data = port;
c41f8cbd 1459 req->erp_action = erp_action;
e60a6d69 1460 erp_action->fsf_req_id = req->req_id;
615f59e0 1461 get_device(&port->dev);
c41f8cbd 1462
287ac01a 1463 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1464 retval = zfcp_fsf_req_send(req);
1da177e4 1465 if (retval) {
c41f8cbd 1466 zfcp_fsf_req_free(req);
e60a6d69 1467 erp_action->fsf_req_id = 0;
615f59e0 1468 put_device(&port->dev);
1da177e4 1469 }
c41f8cbd 1470out:
564e1c86 1471 spin_unlock_bh(&qdio->req_q_lock);
1da177e4
LT
1472 return retval;
1473}
1474
c41f8cbd 1475static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
1da177e4 1476{
c41f8cbd 1477 struct zfcp_port *port = req->data;
1da177e4 1478
c41f8cbd 1479 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1480 return;
1da177e4 1481
c41f8cbd 1482 switch (req->qtcb->header.fsf_status) {
1da177e4 1483 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 1484 zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1", req);
c41f8cbd 1485 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1486 break;
1da177e4 1487 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4 1488 break;
1da177e4 1489 case FSF_GOOD:
5ffd51a5 1490 zfcp_erp_modify_port_status(port, "fscph_2", req,
1da177e4
LT
1491 ZFCP_STATUS_COMMON_OPEN,
1492 ZFCP_CLEAR);
1da177e4 1493 break;
1da177e4 1494 }
1da177e4
LT
1495}
1496
c41f8cbd
SS
1497/**
1498 * zfcp_fsf_close_port - create and send close port request
1499 * @erp_action: pointer to struct zfcp_erp_action
1500 * Returns: 0 on success, error otherwise
1da177e4 1501 */
c41f8cbd 1502int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
1da177e4 1503{
564e1c86 1504 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1505 struct zfcp_fsf_req *req;
1506 int retval = -EIO;
1507
564e1c86 1508 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1509 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
1510 goto out;
1511
564e1c86 1512 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1674b405 1513 SBAL_FLAGS0_TYPE_READ,
564e1c86 1514 qdio->adapter->pool.erp_req);
09a46c6e 1515
025270f0 1516 if (IS_ERR(req)) {
c41f8cbd 1517 retval = PTR_ERR(req);
1da177e4 1518 goto out;
c41f8cbd 1519 }
1da177e4 1520
09a46c6e 1521 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1522 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1523
c41f8cbd
SS
1524 req->handler = zfcp_fsf_close_port_handler;
1525 req->data = erp_action->port;
1526 req->erp_action = erp_action;
1527 req->qtcb->header.port_handle = erp_action->port->handle;
e60a6d69 1528 erp_action->fsf_req_id = req->req_id;
c41f8cbd 1529
287ac01a 1530 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1531 retval = zfcp_fsf_req_send(req);
1da177e4 1532 if (retval) {
c41f8cbd 1533 zfcp_fsf_req_free(req);
e60a6d69 1534 erp_action->fsf_req_id = 0;
1da177e4 1535 }
c41f8cbd 1536out:
564e1c86 1537 spin_unlock_bh(&qdio->req_q_lock);
1da177e4
LT
1538 return retval;
1539}
1540
5ab944f9
SS
1541static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
1542{
bd0072ec 1543 struct zfcp_fc_wka_port *wka_port = req->data;
5ab944f9
SS
1544 struct fsf_qtcb_header *header = &req->qtcb->header;
1545
1546 if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
bd0072ec 1547 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
1548 goto out;
1549 }
1550
1551 switch (header->fsf_status) {
1552 case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1553 dev_warn(&req->adapter->ccw_device->dev,
1554 "Opening WKA port 0x%x failed\n", wka_port->d_id);
dceab655 1555 /* fall through */
5ab944f9
SS
1556 case FSF_ADAPTER_STATUS_AVAILABLE:
1557 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
dceab655 1558 /* fall through */
5ab944f9 1559 case FSF_ACCESS_DENIED:
bd0072ec 1560 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9 1561 break;
5ab944f9
SS
1562 case FSF_GOOD:
1563 wka_port->handle = header->port_handle;
27f492cc
SS
1564 /* fall through */
1565 case FSF_PORT_ALREADY_OPEN:
bd0072ec 1566 wka_port->status = ZFCP_FC_WKA_PORT_ONLINE;
5ab944f9
SS
1567 }
1568out:
1569 wake_up(&wka_port->completion_wq);
1570}
1571
1572/**
1573 * zfcp_fsf_open_wka_port - create and send open wka-port request
bd0072ec 1574 * @wka_port: pointer to struct zfcp_fc_wka_port
5ab944f9
SS
1575 * Returns: 0 on success, error otherwise
1576 */
bd0072ec 1577int zfcp_fsf_open_wka_port(struct zfcp_fc_wka_port *wka_port)
5ab944f9 1578{
564e1c86 1579 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
5ab944f9
SS
1580 struct zfcp_fsf_req *req;
1581 int retval = -EIO;
1582
564e1c86 1583 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1584 if (zfcp_qdio_sbal_get(qdio))
5ab944f9
SS
1585 goto out;
1586
564e1c86 1587 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1674b405 1588 SBAL_FLAGS0_TYPE_READ,
564e1c86 1589 qdio->adapter->pool.erp_req);
09a46c6e 1590
5ab944f9
SS
1591 if (unlikely(IS_ERR(req))) {
1592 retval = PTR_ERR(req);
1593 goto out;
1594 }
1595
09a46c6e 1596 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1597 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
5ab944f9
SS
1598
1599 req->handler = zfcp_fsf_open_wka_port_handler;
800c0cad 1600 hton24(req->qtcb->bottom.support.d_id, wka_port->d_id);
5ab944f9
SS
1601 req->data = wka_port;
1602
1603 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1604 retval = zfcp_fsf_req_send(req);
1605 if (retval)
1606 zfcp_fsf_req_free(req);
1607out:
564e1c86 1608 spin_unlock_bh(&qdio->req_q_lock);
5ab944f9
SS
1609 return retval;
1610}
1611
1612static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
1613{
bd0072ec 1614 struct zfcp_fc_wka_port *wka_port = req->data;
5ab944f9
SS
1615
1616 if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
1617 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
5ffd51a5 1618 zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1", req);
5ab944f9
SS
1619 }
1620
bd0072ec 1621 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
1622 wake_up(&wka_port->completion_wq);
1623}
1624
1625/**
1626 * zfcp_fsf_close_wka_port - create and send close wka port request
bd0072ec 1627 * @wka_port: WKA port to open
5ab944f9
SS
1628 * Returns: 0 on success, error otherwise
1629 */
bd0072ec 1630int zfcp_fsf_close_wka_port(struct zfcp_fc_wka_port *wka_port)
5ab944f9 1631{
564e1c86 1632 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
5ab944f9
SS
1633 struct zfcp_fsf_req *req;
1634 int retval = -EIO;
1635
564e1c86 1636 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1637 if (zfcp_qdio_sbal_get(qdio))
5ab944f9
SS
1638 goto out;
1639
564e1c86 1640 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1674b405 1641 SBAL_FLAGS0_TYPE_READ,
564e1c86 1642 qdio->adapter->pool.erp_req);
09a46c6e 1643
5ab944f9
SS
1644 if (unlikely(IS_ERR(req))) {
1645 retval = PTR_ERR(req);
1646 goto out;
1647 }
1648
09a46c6e 1649 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1650 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
5ab944f9
SS
1651
1652 req->handler = zfcp_fsf_close_wka_port_handler;
1653 req->data = wka_port;
1654 req->qtcb->header.port_handle = wka_port->handle;
1655
1656 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1657 retval = zfcp_fsf_req_send(req);
1658 if (retval)
1659 zfcp_fsf_req_free(req);
1660out:
564e1c86 1661 spin_unlock_bh(&qdio->req_q_lock);
5ab944f9
SS
1662 return retval;
1663}
1664
c41f8cbd 1665static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
1da177e4 1666{
c41f8cbd
SS
1667 struct zfcp_port *port = req->data;
1668 struct fsf_qtcb_header *header = &req->qtcb->header;
1da177e4 1669 struct zfcp_unit *unit;
1da177e4 1670
c41f8cbd 1671 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
a5b11dda 1672 return;
1da177e4 1673
1da177e4 1674 switch (header->fsf_status) {
1da177e4 1675 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 1676 zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1", req);
c41f8cbd 1677 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1678 break;
1da177e4 1679 case FSF_ACCESS_DENIED:
c41f8cbd 1680 zfcp_fsf_access_denied_port(req, port);
1da177e4 1681 break;
1da177e4 1682 case FSF_PORT_BOXED:
5c815d15
CS
1683 /* can't use generic zfcp_erp_modify_port_status because
1684 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
1685 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
ecf0c772
SS
1686 read_lock(&port->unit_list_lock);
1687 list_for_each_entry(unit, &port->unit_list, list)
5c815d15
CS
1688 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1689 &unit->status);
ecf0c772 1690 read_unlock(&port->unit_list_lock);
dfb3cf00 1691 zfcp_erp_port_boxed(port, "fscpph2", req);
4c571c65 1692 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1693 break;
1da177e4 1694 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1695 switch (header->fsf_status_qual.word[0]) {
1696 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
c41f8cbd 1697 /* fall through */
1da177e4 1698 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1699 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1700 break;
1da177e4
LT
1701 }
1702 break;
1da177e4 1703 case FSF_GOOD:
1da177e4
LT
1704 /* can't use generic zfcp_erp_modify_port_status because
1705 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
1706 */
1707 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
ecf0c772
SS
1708 read_lock(&port->unit_list_lock);
1709 list_for_each_entry(unit, &port->unit_list, list)
c41f8cbd
SS
1710 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1711 &unit->status);
ecf0c772 1712 read_unlock(&port->unit_list_lock);
1da177e4 1713 break;
1da177e4 1714 }
1da177e4
LT
1715}
1716
c41f8cbd
SS
1717/**
1718 * zfcp_fsf_close_physical_port - close physical port
1719 * @erp_action: pointer to struct zfcp_erp_action
1720 * Returns: 0 on success
1da177e4 1721 */
c41f8cbd 1722int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
1da177e4 1723{
564e1c86 1724 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1725 struct zfcp_fsf_req *req;
1726 int retval = -EIO;
1727
564e1c86 1728 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1729 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1730 goto out;
1da177e4 1731
564e1c86 1732 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
1674b405 1733 SBAL_FLAGS0_TYPE_READ,
564e1c86 1734 qdio->adapter->pool.erp_req);
09a46c6e 1735
025270f0 1736 if (IS_ERR(req)) {
c41f8cbd
SS
1737 retval = PTR_ERR(req);
1738 goto out;
1739 }
1da177e4 1740
09a46c6e 1741 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1742 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1743
c41f8cbd
SS
1744 req->data = erp_action->port;
1745 req->qtcb->header.port_handle = erp_action->port->handle;
1746 req->erp_action = erp_action;
1747 req->handler = zfcp_fsf_close_physical_port_handler;
e60a6d69 1748 erp_action->fsf_req_id = req->req_id;
c41f8cbd 1749
287ac01a 1750 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1751 retval = zfcp_fsf_req_send(req);
1da177e4 1752 if (retval) {
c41f8cbd 1753 zfcp_fsf_req_free(req);
e60a6d69 1754 erp_action->fsf_req_id = 0;
1da177e4 1755 }
c41f8cbd 1756out:
564e1c86 1757 spin_unlock_bh(&qdio->req_q_lock);
1da177e4
LT
1758 return retval;
1759}
1760
c41f8cbd 1761static void zfcp_fsf_open_unit_handler(struct zfcp_fsf_req *req)
1da177e4 1762{
c41f8cbd
SS
1763 struct zfcp_adapter *adapter = req->adapter;
1764 struct zfcp_unit *unit = req->data;
1765 struct fsf_qtcb_header *header = &req->qtcb->header;
1766 struct fsf_qtcb_bottom_support *bottom = &req->qtcb->bottom.support;
1767 struct fsf_queue_designator *queue_designator =
1768 &header->fsf_status_qual.fsf_queue_designator;
aef4a983 1769 int exclusive, readwrite;
1da177e4 1770
c41f8cbd 1771 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1772 return;
1da177e4 1773
1da177e4 1774 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
b64ddf96 1775 ZFCP_STATUS_COMMON_ACCESS_BOXED |
1da177e4
LT
1776 ZFCP_STATUS_UNIT_SHARED |
1777 ZFCP_STATUS_UNIT_READONLY,
1778 &unit->status);
1779
1da177e4
LT
1780 switch (header->fsf_status) {
1781
1782 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 1783 zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fsouh_1", req);
c41f8cbd 1784 /* fall through */
1da177e4 1785 case FSF_LUN_ALREADY_OPEN:
1da177e4 1786 break;
1da177e4 1787 case FSF_ACCESS_DENIED:
c41f8cbd 1788 zfcp_fsf_access_denied_unit(req, unit);
1da177e4 1789 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
553448f6 1790 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
1da177e4 1791 break;
1da177e4 1792 case FSF_PORT_BOXED:
5ffd51a5 1793 zfcp_erp_port_boxed(unit->port, "fsouh_2", req);
4c571c65 1794 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1795 break;
1da177e4 1796 case FSF_LUN_SHARING_VIOLATION:
c41f8cbd 1797 if (header->fsf_status_qual.word[0])
553448f6 1798 dev_warn(&adapter->ccw_device->dev,
ff3b24fa
CS
1799 "LUN 0x%Lx on port 0x%Lx is already in "
1800 "use by CSS%d, MIF Image ID %x\n",
7ba58c9c
SS
1801 (unsigned long long)unit->fcp_lun,
1802 (unsigned long long)unit->port->wwpn,
ff3b24fa
CS
1803 queue_designator->cssid,
1804 queue_designator->hla);
c41f8cbd 1805 else
553448f6
CS
1806 zfcp_act_eval_err(adapter,
1807 header->fsf_status_qual.word[2]);
5ffd51a5 1808 zfcp_erp_unit_access_denied(unit, "fsouh_3", req);
1da177e4
LT
1809 atomic_clear_mask(ZFCP_STATUS_UNIT_SHARED, &unit->status);
1810 atomic_clear_mask(ZFCP_STATUS_UNIT_READONLY, &unit->status);
c41f8cbd 1811 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1812 break;
1da177e4 1813 case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
c41f8cbd 1814 dev_warn(&adapter->ccw_device->dev,
ff3b24fa
CS
1815 "No handle is available for LUN "
1816 "0x%016Lx on port 0x%016Lx\n",
7ba58c9c
SS
1817 (unsigned long long)unit->fcp_lun,
1818 (unsigned long long)unit->port->wwpn);
5ffd51a5 1819 zfcp_erp_unit_failed(unit, "fsouh_4", req);
c41f8cbd
SS
1820 /* fall through */
1821 case FSF_INVALID_COMMAND_OPTION:
1822 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1823 break;
1da177e4 1824 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1825 switch (header->fsf_status_qual.word[0]) {
1826 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
6f53a2d2 1827 zfcp_fc_test_link(unit->port);
c41f8cbd 1828 /* fall through */
1da177e4 1829 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1830 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1831 break;
1da177e4
LT
1832 }
1833 break;
1834
1da177e4 1835 case FSF_GOOD:
1da177e4 1836 unit->handle = header->lun_handle;
1da177e4 1837 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
aef4a983
MS
1838
1839 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE) &&
1840 (adapter->adapter_features & FSF_FEATURE_LUN_SHARING) &&
6fcf41d1 1841 !zfcp_ccw_priv_sch(adapter)) {
aef4a983
MS
1842 exclusive = (bottom->lun_access_info &
1843 FSF_UNIT_ACCESS_EXCLUSIVE);
1844 readwrite = (bottom->lun_access_info &
1845 FSF_UNIT_ACCESS_OUTBOUND_TRANSFER);
1846
1da177e4
LT
1847 if (!exclusive)
1848 atomic_set_mask(ZFCP_STATUS_UNIT_SHARED,
1849 &unit->status);
1850
1851 if (!readwrite) {
1852 atomic_set_mask(ZFCP_STATUS_UNIT_READONLY,
1853 &unit->status);
c41f8cbd 1854 dev_info(&adapter->ccw_device->dev,
ff3b24fa
CS
1855 "SCSI device at LUN 0x%016Lx on port "
1856 "0x%016Lx opened read-only\n",
7ba58c9c
SS
1857 (unsigned long long)unit->fcp_lun,
1858 (unsigned long long)unit->port->wwpn);
1da177e4
LT
1859 }
1860
1861 if (exclusive && !readwrite) {
c41f8cbd 1862 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
1863 "Exclusive read-only access not "
1864 "supported (unit 0x%016Lx, "
1865 "port 0x%016Lx)\n",
7ba58c9c
SS
1866 (unsigned long long)unit->fcp_lun,
1867 (unsigned long long)unit->port->wwpn);
5ffd51a5 1868 zfcp_erp_unit_failed(unit, "fsouh_5", req);
c41f8cbd 1869 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
5ffd51a5 1870 zfcp_erp_unit_shutdown(unit, 0, "fsouh_6", req);
1da177e4 1871 } else if (!exclusive && readwrite) {
c41f8cbd 1872 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
1873 "Shared read-write access not "
1874 "supported (unit 0x%016Lx, port "
27c3f0a6 1875 "0x%016Lx)\n",
7ba58c9c
SS
1876 (unsigned long long)unit->fcp_lun,
1877 (unsigned long long)unit->port->wwpn);
5ffd51a5 1878 zfcp_erp_unit_failed(unit, "fsouh_7", req);
c41f8cbd 1879 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
5ffd51a5 1880 zfcp_erp_unit_shutdown(unit, 0, "fsouh_8", req);
1da177e4
LT
1881 }
1882 }
1da177e4 1883 break;
1da177e4 1884 }
1da177e4
LT
1885}
1886
c41f8cbd
SS
1887/**
1888 * zfcp_fsf_open_unit - open unit
1889 * @erp_action: pointer to struct zfcp_erp_action
1890 * Returns: 0 on success, error otherwise
1da177e4 1891 */
c41f8cbd 1892int zfcp_fsf_open_unit(struct zfcp_erp_action *erp_action)
1da177e4 1893{
c41f8cbd 1894 struct zfcp_adapter *adapter = erp_action->adapter;
564e1c86 1895 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
1896 struct zfcp_fsf_req *req;
1897 int retval = -EIO;
1898
564e1c86 1899 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1900 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1901 goto out;
1da177e4 1902
564e1c86 1903 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
1674b405 1904 SBAL_FLAGS0_TYPE_READ,
a4623c46 1905 adapter->pool.erp_req);
09a46c6e 1906
025270f0 1907 if (IS_ERR(req)) {
c41f8cbd
SS
1908 retval = PTR_ERR(req);
1909 goto out;
1910 }
1911
09a46c6e 1912 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1913 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1914
c41f8cbd
SS
1915 req->qtcb->header.port_handle = erp_action->port->handle;
1916 req->qtcb->bottom.support.fcp_lun = erp_action->unit->fcp_lun;
1917 req->handler = zfcp_fsf_open_unit_handler;
1918 req->data = erp_action->unit;
1919 req->erp_action = erp_action;
e60a6d69 1920 erp_action->fsf_req_id = req->req_id;
c41f8cbd
SS
1921
1922 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
1923 req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;
1924
287ac01a 1925 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1926 retval = zfcp_fsf_req_send(req);
1da177e4 1927 if (retval) {
c41f8cbd 1928 zfcp_fsf_req_free(req);
e60a6d69 1929 erp_action->fsf_req_id = 0;
1da177e4 1930 }
c41f8cbd 1931out:
564e1c86 1932 spin_unlock_bh(&qdio->req_q_lock);
1da177e4
LT
1933 return retval;
1934}
1935
c41f8cbd 1936static void zfcp_fsf_close_unit_handler(struct zfcp_fsf_req *req)
1da177e4 1937{
c41f8cbd 1938 struct zfcp_unit *unit = req->data;
1da177e4 1939
c41f8cbd 1940 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1941 return;
1da177e4 1942
c41f8cbd 1943 switch (req->qtcb->header.fsf_status) {
1da177e4 1944 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 1945 zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fscuh_1", req);
c41f8cbd 1946 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1947 break;
1da177e4 1948 case FSF_LUN_HANDLE_NOT_VALID:
5ffd51a5 1949 zfcp_erp_port_reopen(unit->port, 0, "fscuh_2", req);
c41f8cbd 1950 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1951 break;
1da177e4 1952 case FSF_PORT_BOXED:
5ffd51a5 1953 zfcp_erp_port_boxed(unit->port, "fscuh_3", req);
4c571c65 1954 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1955 break;
1da177e4 1956 case FSF_ADAPTER_STATUS_AVAILABLE:
c41f8cbd 1957 switch (req->qtcb->header.fsf_status_qual.word[0]) {
1da177e4 1958 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
6f53a2d2 1959 zfcp_fc_test_link(unit->port);
c41f8cbd 1960 /* fall through */
1da177e4 1961 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1962 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1963 break;
1964 }
1965 break;
1da177e4 1966 case FSF_GOOD:
1da177e4 1967 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &unit->status);
1da177e4 1968 break;
1da177e4 1969 }
1da177e4
LT
1970}
1971
1972/**
c41f8cbd
SS
1973 * zfcp_fsf_close_unit - close zfcp unit
1974 * @erp_action: pointer to struct zfcp_unit
1975 * Returns: 0 on success, error otherwise
1da177e4 1976 */
c41f8cbd 1977int zfcp_fsf_close_unit(struct zfcp_erp_action *erp_action)
1da177e4 1978{
564e1c86 1979 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1980 struct zfcp_fsf_req *req;
1981 int retval = -EIO;
1da177e4 1982
564e1c86 1983 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 1984 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1985 goto out;
09a46c6e 1986
564e1c86 1987 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
1674b405 1988 SBAL_FLAGS0_TYPE_READ,
564e1c86 1989 qdio->adapter->pool.erp_req);
09a46c6e 1990
025270f0 1991 if (IS_ERR(req)) {
c41f8cbd
SS
1992 retval = PTR_ERR(req);
1993 goto out;
1994 }
1da177e4 1995
09a46c6e 1996 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1997 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1998
c41f8cbd
SS
1999 req->qtcb->header.port_handle = erp_action->port->handle;
2000 req->qtcb->header.lun_handle = erp_action->unit->handle;
2001 req->handler = zfcp_fsf_close_unit_handler;
2002 req->data = erp_action->unit;
2003 req->erp_action = erp_action;
e60a6d69 2004 erp_action->fsf_req_id = req->req_id;
fdf23452 2005
287ac01a 2006 zfcp_fsf_start_erp_timer(req);
c41f8cbd
SS
2007 retval = zfcp_fsf_req_send(req);
2008 if (retval) {
2009 zfcp_fsf_req_free(req);
e60a6d69 2010 erp_action->fsf_req_id = 0;
c41f8cbd
SS
2011 }
2012out:
564e1c86 2013 spin_unlock_bh(&qdio->req_q_lock);
c41f8cbd 2014 return retval;
1da177e4
LT
2015}
2016
c9615858
CS
2017static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
2018{
2019 lat_rec->sum += lat;
c41f8cbd
SS
2020 lat_rec->min = min(lat_rec->min, lat);
2021 lat_rec->max = max(lat_rec->max, lat);
c9615858
CS
2022}
2023
d9742b42 2024static void zfcp_fsf_req_trace(struct zfcp_fsf_req *req, struct scsi_cmnd *scsi)
c9615858 2025{
d9742b42
CS
2026 struct fsf_qual_latency_info *lat_in;
2027 struct latency_cont *lat = NULL;
c41f8cbd 2028 struct zfcp_unit *unit = req->unit;
d9742b42
CS
2029 struct zfcp_blk_drv_data blktrc;
2030 int ticks = req->adapter->timer_ticks;
c9615858 2031
d9742b42 2032 lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
c9615858 2033
d9742b42
CS
2034 blktrc.flags = 0;
2035 blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
2036 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
2037 blktrc.flags |= ZFCP_BLK_REQ_ERROR;
706eca49 2038 blktrc.inb_usage = 0;
34c2b712 2039 blktrc.outb_usage = req->qdio_req.qdio_outb_usage;
d9742b42 2040
5bbf297c
CS
2041 if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA &&
2042 !(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
d9742b42
CS
2043 blktrc.flags |= ZFCP_BLK_LAT_VALID;
2044 blktrc.channel_lat = lat_in->channel_lat * ticks;
2045 blktrc.fabric_lat = lat_in->fabric_lat * ticks;
2046
2047 switch (req->qtcb->bottom.io.data_direction) {
ef3eb71d
FB
2048 case FSF_DATADIR_DIF_READ_STRIP:
2049 case FSF_DATADIR_DIF_READ_CONVERT:
d9742b42
CS
2050 case FSF_DATADIR_READ:
2051 lat = &unit->latencies.read;
2052 break;
ef3eb71d
FB
2053 case FSF_DATADIR_DIF_WRITE_INSERT:
2054 case FSF_DATADIR_DIF_WRITE_CONVERT:
d9742b42
CS
2055 case FSF_DATADIR_WRITE:
2056 lat = &unit->latencies.write;
2057 break;
2058 case FSF_DATADIR_CMND:
2059 lat = &unit->latencies.cmd;
2060 break;
2061 }
1da177e4 2062
d9742b42
CS
2063 if (lat) {
2064 spin_lock(&unit->latencies.lock);
2065 zfcp_fsf_update_lat(&lat->channel, lat_in->channel_lat);
2066 zfcp_fsf_update_lat(&lat->fabric, lat_in->fabric_lat);
2067 lat->counter++;
2068 spin_unlock(&unit->latencies.lock);
2069 }
0997f1c5 2070 }
0997f1c5 2071
d9742b42
CS
2072 blk_add_driver_data(scsi->request->q, scsi->request, &blktrc,
2073 sizeof(blktrc));
0997f1c5 2074}
0997f1c5 2075
c41f8cbd 2076static void zfcp_fsf_send_fcp_command_task_handler(struct zfcp_fsf_req *req)
1da177e4 2077{
0ac55aa9 2078 struct scsi_cmnd *scpnt;
4318e08c 2079 struct fcp_resp_with_ext *fcp_rsp;
1da177e4 2080 unsigned long flags;
1da177e4 2081
c41f8cbd
SS
2082 read_lock_irqsave(&req->adapter->abort_lock, flags);
2083
0ac55aa9
SS
2084 scpnt = req->data;
2085 if (unlikely(!scpnt)) {
2086 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2087 return;
2088 }
2089
c41f8cbd 2090 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
4c571c65 2091 set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
1da177e4
LT
2092 goto skip_fsfstatus;
2093 }
2094
ef3eb71d
FB
2095 switch (req->qtcb->header.fsf_status) {
2096 case FSF_INCONSISTENT_PROT_DATA:
2097 case FSF_INVALID_PROT_PARM:
2098 set_host_byte(scpnt, DID_ERROR);
2099 goto skip_fsfstatus;
2100 case FSF_BLOCK_GUARD_CHECK_FAILURE:
2101 zfcp_scsi_dif_sense_error(scpnt, 0x1);
2102 goto skip_fsfstatus;
2103 case FSF_APP_TAG_CHECK_FAILURE:
2104 zfcp_scsi_dif_sense_error(scpnt, 0x2);
2105 goto skip_fsfstatus;
2106 case FSF_REF_TAG_CHECK_FAILURE:
2107 zfcp_scsi_dif_sense_error(scpnt, 0x3);
2108 goto skip_fsfstatus;
2109 }
4318e08c
CS
2110 fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
2111 zfcp_fc_eval_fcp_rsp(fcp_rsp, scpnt);
1da177e4 2112
c41f8cbd 2113skip_fsfstatus:
5bbf297c 2114 zfcp_fsf_req_trace(req, scpnt);
ab72528a 2115 zfcp_dbf_scsi_result(req->adapter->dbf, scpnt, req);
1da177e4 2116
1da177e4 2117 scpnt->host_scribble = NULL;
1da177e4 2118 (scpnt->scsi_done) (scpnt);
1da177e4
LT
2119 /*
2120 * We must hold this lock until scsi_done has been called.
2121 * Otherwise we may call scsi_done after abort regarding this
2122 * command has completed.
2123 * Note: scsi_done must not block!
2124 */
c41f8cbd 2125 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
1da177e4
LT
2126}
2127
c41f8cbd 2128static void zfcp_fsf_send_fcp_ctm_handler(struct zfcp_fsf_req *req)
1da177e4 2129{
4318e08c
CS
2130 struct fcp_resp_with_ext *fcp_rsp;
2131 struct fcp_resp_rsp_info *rsp_info;
1da177e4 2132
4318e08c
CS
2133 fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
2134 rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
2135
2136 if ((rsp_info->rsp_code != FCP_TMF_CMPL) ||
c41f8cbd
SS
2137 (req->status & ZFCP_STATUS_FSFREQ_ERROR))
2138 req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
2139}
2140
2141
2142static void zfcp_fsf_send_fcp_command_handler(struct zfcp_fsf_req *req)
2143{
2144 struct zfcp_unit *unit;
2145 struct fsf_qtcb_header *header = &req->qtcb->header;
2146
2147 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT))
2148 unit = req->data;
2149 else
2150 unit = req->unit;
2151
2152 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
1da177e4 2153 goto skip_fsfstatus;
1da177e4 2154
c41f8cbd
SS
2155 switch (header->fsf_status) {
2156 case FSF_HANDLE_MISMATCH:
2157 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 2158 zfcp_erp_adapter_reopen(unit->port->adapter, 0, "fssfch1", req);
c41f8cbd
SS
2159 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2160 break;
2161 case FSF_FCPLUN_NOT_VALID:
2162 case FSF_LUN_HANDLE_NOT_VALID:
5ffd51a5 2163 zfcp_erp_port_reopen(unit->port, 0, "fssfch2", req);
c41f8cbd 2164 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 2165 break;
c41f8cbd
SS
2166 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
2167 zfcp_fsf_class_not_supp(req);
1da177e4 2168 break;
c41f8cbd
SS
2169 case FSF_ACCESS_DENIED:
2170 zfcp_fsf_access_denied_unit(req, unit);
2171 break;
2172 case FSF_DIRECTION_INDICATOR_NOT_VALID:
2173 dev_err(&req->adapter->ccw_device->dev,
ff3b24fa
CS
2174 "Incorrect direction %d, unit 0x%016Lx on port "
2175 "0x%016Lx closed\n",
c41f8cbd 2176 req->qtcb->bottom.io.data_direction,
7ba58c9c
SS
2177 (unsigned long long)unit->fcp_lun,
2178 (unsigned long long)unit->port->wwpn);
5ffd51a5
SS
2179 zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch3",
2180 req);
c41f8cbd
SS
2181 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2182 break;
2183 case FSF_CMND_LENGTH_NOT_VALID:
2184 dev_err(&req->adapter->ccw_device->dev,
ff3b24fa
CS
2185 "Incorrect CDB length %d, unit 0x%016Lx on "
2186 "port 0x%016Lx closed\n",
c41f8cbd 2187 req->qtcb->bottom.io.fcp_cmnd_length,
7ba58c9c
SS
2188 (unsigned long long)unit->fcp_lun,
2189 (unsigned long long)unit->port->wwpn);
5ffd51a5
SS
2190 zfcp_erp_adapter_shutdown(unit->port->adapter, 0, "fssfch4",
2191 req);
c41f8cbd
SS
2192 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2193 break;
2194 case FSF_PORT_BOXED:
5ffd51a5 2195 zfcp_erp_port_boxed(unit->port, "fssfch5", req);
4c571c65 2196 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
2197 break;
2198 case FSF_LUN_BOXED:
5ffd51a5 2199 zfcp_erp_unit_boxed(unit, "fssfch6", req);
4c571c65 2200 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
2201 break;
2202 case FSF_ADAPTER_STATUS_AVAILABLE:
2203 if (header->fsf_status_qual.word[0] ==
2204 FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
6f53a2d2 2205 zfcp_fc_test_link(unit->port);
c41f8cbd 2206 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 2207 break;
1da177e4 2208 }
c41f8cbd
SS
2209skip_fsfstatus:
2210 if (req->status & ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT)
2211 zfcp_fsf_send_fcp_ctm_handler(req);
2212 else {
2213 zfcp_fsf_send_fcp_command_task_handler(req);
2214 req->unit = NULL;
615f59e0 2215 put_device(&unit->dev);
c41f8cbd 2216 }
1da177e4
LT
2217}
2218
ef3eb71d
FB
2219static int zfcp_fsf_set_data_dir(struct scsi_cmnd *scsi_cmnd, u32 *data_dir)
2220{
2221 switch (scsi_get_prot_op(scsi_cmnd)) {
2222 case SCSI_PROT_NORMAL:
2223 switch (scsi_cmnd->sc_data_direction) {
2224 case DMA_NONE:
2225 *data_dir = FSF_DATADIR_CMND;
2226 break;
2227 case DMA_FROM_DEVICE:
2228 *data_dir = FSF_DATADIR_READ;
2229 break;
2230 case DMA_TO_DEVICE:
2231 *data_dir = FSF_DATADIR_WRITE;
2232 break;
2233 case DMA_BIDIRECTIONAL:
2234 return -EINVAL;
2235 }
2236 break;
2237
2238 case SCSI_PROT_READ_STRIP:
2239 *data_dir = FSF_DATADIR_DIF_READ_STRIP;
2240 break;
2241 case SCSI_PROT_WRITE_INSERT:
2242 *data_dir = FSF_DATADIR_DIF_WRITE_INSERT;
2243 break;
2244 case SCSI_PROT_READ_PASS:
2245 *data_dir = FSF_DATADIR_DIF_READ_CONVERT;
2246 break;
2247 case SCSI_PROT_WRITE_PASS:
2248 *data_dir = FSF_DATADIR_DIF_WRITE_CONVERT;
2249 break;
2250 default:
2251 return -EINVAL;
2252 }
2253
2254 return 0;
2255}
2256
c41f8cbd
SS
2257/**
2258 * zfcp_fsf_send_fcp_command_task - initiate an FCP command (for a SCSI command)
c41f8cbd
SS
2259 * @unit: unit where command is sent to
2260 * @scsi_cmnd: scsi command to be sent
1da177e4 2261 */
63caf367
CS
2262int zfcp_fsf_send_fcp_command_task(struct zfcp_unit *unit,
2263 struct scsi_cmnd *scsi_cmnd)
1da177e4 2264{
c41f8cbd 2265 struct zfcp_fsf_req *req;
4318e08c 2266 struct fcp_cmnd *fcp_cmnd;
bc90c863 2267 unsigned int sbtype = SBAL_FLAGS0_TYPE_READ;
ef3eb71d 2268 int real_bytes, retval = -EIO, dix_bytes = 0;
63caf367 2269 struct zfcp_adapter *adapter = unit->port->adapter;
564e1c86 2270 struct zfcp_qdio *qdio = adapter->qdio;
ef3eb71d 2271 struct fsf_qtcb_bottom_io *io;
1da177e4 2272
c41f8cbd
SS
2273 if (unlikely(!(atomic_read(&unit->status) &
2274 ZFCP_STATUS_COMMON_UNBLOCKED)))
2275 return -EBUSY;
1da177e4 2276
564e1c86 2277 spin_lock(&qdio->req_q_lock);
706eca49 2278 if (atomic_read(&qdio->req_q_free) <= 0) {
564e1c86 2279 atomic_inc(&qdio->req_q_full);
c41f8cbd 2280 goto out;
8fdf30d5 2281 }
09a46c6e 2282
1674b405
CS
2283 if (scsi_cmnd->sc_data_direction == DMA_TO_DEVICE)
2284 sbtype = SBAL_FLAGS0_TYPE_WRITE;
2285
564e1c86 2286 req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
1674b405 2287 sbtype, adapter->pool.scsi_req);
09a46c6e 2288
025270f0 2289 if (IS_ERR(req)) {
c41f8cbd
SS
2290 retval = PTR_ERR(req);
2291 goto out;
2292 }
2293
ef3eb71d
FB
2294 scsi_cmnd->host_scribble = (unsigned char *) req->req_id;
2295
2296 io = &req->qtcb->bottom.io;
09a46c6e 2297 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
c41f8cbd
SS
2298 req->unit = unit;
2299 req->data = scsi_cmnd;
2300 req->handler = zfcp_fsf_send_fcp_command_handler;
2301 req->qtcb->header.lun_handle = unit->handle;
2302 req->qtcb->header.port_handle = unit->port->handle;
ef3eb71d
FB
2303 io->service_class = FSF_CLASS_3;
2304 io->fcp_cmnd_length = FCP_CMND_LEN;
c41f8cbd 2305
ef3eb71d
FB
2306 if (scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) {
2307 io->data_block_length = scsi_cmnd->device->sector_size;
2308 io->ref_tag_value = scsi_get_lba(scsi_cmnd) & 0xFFFFFFFF;
1da177e4
LT
2309 }
2310
ef3eb71d
FB
2311 zfcp_fsf_set_data_dir(scsi_cmnd, &io->data_direction);
2312
1674b405
CS
2313 get_device(&unit->dev);
2314
4318e08c
CS
2315 fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
2316 zfcp_fc_scsi_to_fcp(fcp_cmnd, scsi_cmnd);
1da177e4 2317
ef3eb71d
FB
2318 if (scsi_prot_sg_count(scsi_cmnd)) {
2319 zfcp_qdio_set_data_div(qdio, &req->qdio_req,
2320 scsi_prot_sg_count(scsi_cmnd));
2321 dix_bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2322 scsi_prot_sglist(scsi_cmnd));
2323 io->prot_data_length = dix_bytes;
2324 }
2325
1674b405 2326 real_bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
01b04759 2327 scsi_sglist(scsi_cmnd));
ef3eb71d
FB
2328
2329 if (unlikely(real_bytes < 0) || unlikely(dix_bytes < 0))
c41f8cbd 2330 goto failed_scsi_cmnd;
1da177e4 2331
ef3eb71d
FB
2332 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
2333
c41f8cbd
SS
2334 retval = zfcp_fsf_req_send(req);
2335 if (unlikely(retval))
2336 goto failed_scsi_cmnd;
1da177e4 2337
c41f8cbd 2338 goto out;
1da177e4 2339
c41f8cbd 2340failed_scsi_cmnd:
615f59e0 2341 put_device(&unit->dev);
c41f8cbd
SS
2342 zfcp_fsf_req_free(req);
2343 scsi_cmnd->host_scribble = NULL;
2344out:
564e1c86 2345 spin_unlock(&qdio->req_q_lock);
c41f8cbd 2346 return retval;
1da177e4
LT
2347}
2348
2349/**
c41f8cbd 2350 * zfcp_fsf_send_fcp_ctm - send SCSI task management command
c41f8cbd
SS
2351 * @unit: pointer to struct zfcp_unit
2352 * @tm_flags: unsigned byte for task management flags
c41f8cbd 2353 * Returns: on success pointer to struct fsf_req, NULL otherwise
1da177e4 2354 */
63caf367 2355struct zfcp_fsf_req *zfcp_fsf_send_fcp_ctm(struct zfcp_unit *unit, u8 tm_flags)
1da177e4 2356{
c41f8cbd 2357 struct zfcp_fsf_req *req = NULL;
4318e08c 2358 struct fcp_cmnd *fcp_cmnd;
564e1c86 2359 struct zfcp_qdio *qdio = unit->port->adapter->qdio;
1da177e4 2360
c41f8cbd
SS
2361 if (unlikely(!(atomic_read(&unit->status) &
2362 ZFCP_STATUS_COMMON_UNBLOCKED)))
2363 return NULL;
1da177e4 2364
564e1c86 2365 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 2366 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 2367 goto out;
09a46c6e 2368
564e1c86 2369 req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
1674b405 2370 SBAL_FLAGS0_TYPE_WRITE,
564e1c86 2371 qdio->adapter->pool.scsi_req);
09a46c6e 2372
633528c3
SS
2373 if (IS_ERR(req)) {
2374 req = NULL;
c41f8cbd 2375 goto out;
633528c3 2376 }
1da177e4 2377
c41f8cbd
SS
2378 req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
2379 req->data = unit;
2380 req->handler = zfcp_fsf_send_fcp_command_handler;
2381 req->qtcb->header.lun_handle = unit->handle;
2382 req->qtcb->header.port_handle = unit->port->handle;
2383 req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2384 req->qtcb->bottom.io.service_class = FSF_CLASS_3;
4318e08c 2385 req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
c41f8cbd 2386
1674b405 2387 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 2388
4318e08c
CS
2389 fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
2390 zfcp_fc_fcp_tm(fcp_cmnd, unit->device, tm_flags);
1da177e4 2391
c41f8cbd
SS
2392 zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
2393 if (!zfcp_fsf_req_send(req))
2394 goto out;
1da177e4 2395
c41f8cbd
SS
2396 zfcp_fsf_req_free(req);
2397 req = NULL;
2398out:
564e1c86 2399 spin_unlock_bh(&qdio->req_q_lock);
c41f8cbd
SS
2400 return req;
2401}
1da177e4 2402
c41f8cbd
SS
2403static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
2404{
1da177e4
LT
2405}
2406
c41f8cbd
SS
2407/**
2408 * zfcp_fsf_control_file - control file upload/download
2409 * @adapter: pointer to struct zfcp_adapter
2410 * @fsf_cfdc: pointer to struct zfcp_fsf_cfdc
2411 * Returns: on success pointer to struct zfcp_fsf_req, NULL otherwise
1da177e4 2412 */
c41f8cbd
SS
2413struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
2414 struct zfcp_fsf_cfdc *fsf_cfdc)
1da177e4 2415{
564e1c86 2416 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
2417 struct zfcp_fsf_req *req = NULL;
2418 struct fsf_qtcb_bottom_support *bottom;
2419 int direction, retval = -EIO, bytes;
2420
2421 if (!(adapter->adapter_features & FSF_FEATURE_CFDC))
2422 return ERR_PTR(-EOPNOTSUPP);
2423
2424 switch (fsf_cfdc->command) {
2425 case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
2426 direction = SBAL_FLAGS0_TYPE_WRITE;
2427 break;
2428 case FSF_QTCB_UPLOAD_CONTROL_FILE:
2429 direction = SBAL_FLAGS0_TYPE_READ;
2430 break;
2431 default:
2432 return ERR_PTR(-EINVAL);
2433 }
1da177e4 2434
564e1c86 2435 spin_lock_bh(&qdio->req_q_lock);
6b9e1520 2436 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 2437 goto out;
1da177e4 2438
1674b405 2439 req = zfcp_fsf_req_create(qdio, fsf_cfdc->command, direction, NULL);
025270f0 2440 if (IS_ERR(req)) {
c41f8cbd
SS
2441 retval = -EPERM;
2442 goto out;
2443 }
1da177e4 2444
c41f8cbd 2445 req->handler = zfcp_fsf_control_file_handler;
1da177e4 2446
c41f8cbd
SS
2447 bottom = &req->qtcb->bottom.support;
2448 bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
2449 bottom->option = fsf_cfdc->option;
8a36e453 2450
01b04759
SS
2451 bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, fsf_cfdc->sg);
2452
c41f8cbd 2453 if (bytes != ZFCP_CFDC_MAX_SIZE) {
c41f8cbd
SS
2454 zfcp_fsf_req_free(req);
2455 goto out;
2456 }
ef3eb71d 2457 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
1da177e4 2458
c41f8cbd
SS
2459 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
2460 retval = zfcp_fsf_req_send(req);
2461out:
564e1c86 2462 spin_unlock_bh(&qdio->req_q_lock);
8a36e453 2463
c41f8cbd 2464 if (!retval) {
058b8647 2465 wait_for_completion(&req->completion);
c41f8cbd 2466 return req;
1da177e4 2467 }
c41f8cbd 2468 return ERR_PTR(retval);
1da177e4 2469}
bd63eaf4
SS
2470
2471/**
2472 * zfcp_fsf_reqid_check - validate req_id contained in SBAL returned by QDIO
2473 * @adapter: pointer to struct zfcp_adapter
2474 * @sbal_idx: response queue index of SBAL to be processed
2475 */
564e1c86 2476void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
bd63eaf4 2477{
564e1c86 2478 struct zfcp_adapter *adapter = qdio->adapter;
706eca49 2479 struct qdio_buffer *sbal = qdio->res_q[sbal_idx];
bd63eaf4
SS
2480 struct qdio_buffer_element *sbale;
2481 struct zfcp_fsf_req *fsf_req;
b6bd2fb9 2482 unsigned long req_id;
bd63eaf4
SS
2483 int idx;
2484
2485 for (idx = 0; idx < QDIO_MAX_ELEMENTS_PER_BUFFER; idx++) {
2486
2487 sbale = &sbal->element[idx];
2488 req_id = (unsigned long) sbale->addr;
b6bd2fb9 2489 fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
bd63eaf4 2490
339f4f4e 2491 if (!fsf_req) {
bd63eaf4
SS
2492 /*
2493 * Unknown request means that we have potentially memory
2494 * corruption and must stop the machine immediately.
2495 */
339f4f4e 2496 zfcp_qdio_siosl(adapter);
bd63eaf4
SS
2497 panic("error: unknown req_id (%lx) on adapter %s.\n",
2498 req_id, dev_name(&adapter->ccw_device->dev));
339f4f4e 2499 }
bd63eaf4 2500
34c2b712 2501 fsf_req->qdio_req.sbal_response = sbal_idx;
bd63eaf4
SS
2502 zfcp_fsf_req_complete(fsf_req);
2503
2504 if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
2505 break;
2506 }
2507}