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