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