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