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