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