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