]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/s390/scsi/zfcp_fsf.c
Merge branches 'sh/pio-death', 'sh/nommu', 'sh/clkfwk', 'sh/core' and 'sh/intc-extens...
[net-next-2.6.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_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");
5ffd51a5 68 zfcp_erp_adapter_shutdown(req->adapter, 0, "fscns_1", req);
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) {
5ffd51a5 101 zfcp_erp_port_reopen(port, 0, "fssrpc1", req);
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) {
5771710b 214 zfcp_dbf_hba_fsf_unsol("dism", adapter->dbf, sr_buf);
a4623c46 215 mempool_free(sr_buf, adapter->pool.status_read_data);
c41f8cbd
SS
216 zfcp_fsf_req_free(req);
217 return;
218 }
1da177e4 219
5771710b 220 zfcp_dbf_hba_fsf_unsol("read", adapter->dbf, sr_buf);
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");
5771710b 235 zfcp_dbf_hba_berr(adapter->dbf, 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,
5ffd51a5 250 "fssrh_2", req);
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);
5ffd51a5 290 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfsqe1", req);
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);
5ffd51a5 307 zfcp_erp_adapter_shutdown(req->adapter, 0, "fsfse_1", req);
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]);
5ffd51a5 338 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_1", req);
c41f8cbd
SS
339 break;
340 case FSF_PROT_ERROR_STATE:
341 case FSF_PROT_SEQ_NUMB_ERROR:
5ffd51a5 342 zfcp_erp_adapter_reopen(adapter, 0, "fspse_2", req);
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");
5ffd51a5 348 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_3", req);
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);
5ffd51a5 358 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_4", req);
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 */
5ffd51a5 363 zfcp_erp_adapter_reopen(adapter, 0, "fspse_6", req);
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
SS
371 ZFCP_STATUS_COMMON_ERP_FAILED,
372 "fspse_8", req);
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);
5ffd51a5 379 zfcp_erp_adapter_shutdown(adapter, 0, "fspse_9", req);
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");
5ffd51a5 485 zfcp_erp_adapter_shutdown(adapter, 0, "fsece_1", req);
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);
5ffd51a5 521 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh1", req);
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:
5ffd51a5 539 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh3", req);
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");
5ffd51a5 555 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh4", req);
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");
5ffd51a5 562 zfcp_erp_adapter_shutdown(adapter, 0, "fsecdh5", req);
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);
5ffd51a5 703 zfcp_erp_adapter_reopen(adapter, 0, "fsrs__1", req);
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:
a4623c46 757 mempool_free(sr_buf, adapter->pool.status_read_data);
c41f8cbd
SS
758failed_buf:
759 zfcp_fsf_req_free(req);
5771710b 760 zfcp_dbf_hba_fsf_unsol("fail", adapter->dbf, NULL);
c41f8cbd 761out:
44a24cb3 762 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd
SS
763 return retval;
764}
765
766static void zfcp_fsf_abort_fcp_command_handler(struct zfcp_fsf_req *req)
767{
b62a8d9b
CS
768 struct scsi_device *sdev = req->data;
769 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
c41f8cbd
SS
770 union fsf_status_qual *fsq = &req->qtcb->header.fsf_status_qual;
771
772 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
773 return;
774
775 switch (req->qtcb->header.fsf_status) {
1da177e4 776 case FSF_PORT_HANDLE_NOT_VALID:
c41f8cbd 777 if (fsq->word[0] == fsq->word[1]) {
b62a8d9b 778 zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0,
5ffd51a5 779 "fsafch1", req);
c41f8cbd 780 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
781 }
782 break;
1da177e4 783 case FSF_LUN_HANDLE_NOT_VALID:
c41f8cbd 784 if (fsq->word[0] == fsq->word[1]) {
b62a8d9b
CS
785 zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fsafch2",
786 req);
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,
797 ZFCP_STATUS_COMMON_ERP_FAILED, "fsafch3",
798 req);
4c571c65 799 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 800 break;
1da177e4 801 case FSF_LUN_BOXED:
edaed859
SS
802 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
803 zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
804 "fsafch4", req);
4c571c65 805 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 806 break;
1da177e4 807 case FSF_ADAPTER_STATUS_AVAILABLE:
c41f8cbd 808 switch (fsq->word[0]) {
1da177e4 809 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
b62a8d9b 810 zfcp_fc_test_link(zfcp_sdev->port);
dceab655 811 /* fall through */
1da177e4 812 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 813 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 814 break;
1da177e4
LT
815 }
816 break;
1da177e4 817 case FSF_GOOD:
c41f8cbd 818 req->status |= ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED;
1da177e4 819 break;
1da177e4 820 }
1da177e4
LT
821}
822
823/**
b62a8d9b
CS
824 * zfcp_fsf_abort_fcp_cmnd - abort running SCSI command
825 * @scmnd: The SCSI command to abort
c41f8cbd 826 * Returns: pointer to struct zfcp_fsf_req
1da177e4 827 */
1da177e4 828
b62a8d9b 829struct zfcp_fsf_req *zfcp_fsf_abort_fcp_cmnd(struct scsi_cmnd *scmnd)
1da177e4 830{
c41f8cbd 831 struct zfcp_fsf_req *req = NULL;
b62a8d9b
CS
832 struct scsi_device *sdev = scmnd->device;
833 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
834 struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
835 unsigned long old_req_id = (unsigned long) scmnd->host_scribble;
8a36e453 836
44a24cb3 837 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 838 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 839 goto out;
564e1c86 840 req = zfcp_fsf_req_create(qdio, FSF_QTCB_ABORT_FCP_CMND,
1674b405 841 SBAL_FLAGS0_TYPE_READ,
564e1c86 842 qdio->adapter->pool.scsi_abort);
633528c3
SS
843 if (IS_ERR(req)) {
844 req = NULL;
c41f8cbd 845 goto out;
633528c3 846 }
2abbe866 847
b62a8d9b 848 if (unlikely(!(atomic_read(&zfcp_sdev->status) &
c41f8cbd
SS
849 ZFCP_STATUS_COMMON_UNBLOCKED)))
850 goto out_error_free;
1da177e4 851
1674b405 852 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 853
b62a8d9b 854 req->data = zfcp_sdev;
c41f8cbd 855 req->handler = zfcp_fsf_abort_fcp_command_handler;
b62a8d9b
CS
856 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
857 req->qtcb->header.port_handle = zfcp_sdev->port->handle;
c41f8cbd
SS
858 req->qtcb->bottom.support.req_handle = (u64) old_req_id;
859
860 zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
861 if (!zfcp_fsf_req_send(req))
862 goto out;
863
864out_error_free:
865 zfcp_fsf_req_free(req);
866 req = NULL;
867out:
44a24cb3 868 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 869 return req;
1da177e4
LT
870}
871
c41f8cbd 872static void zfcp_fsf_send_ct_handler(struct zfcp_fsf_req *req)
1da177e4 873{
c41f8cbd 874 struct zfcp_adapter *adapter = req->adapter;
7c7dc196 875 struct zfcp_fsf_ct_els *ct = req->data;
c41f8cbd 876 struct fsf_qtcb_header *header = &req->qtcb->header;
1da177e4 877
7c7dc196 878 ct->status = -EINVAL;
1da177e4 879
c41f8cbd 880 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1da177e4
LT
881 goto skip_fsfstatus;
882
1da177e4 883 switch (header->fsf_status) {
1da177e4 884 case FSF_GOOD:
5771710b 885 zfcp_dbf_san_ct_response(req);
7c7dc196 886 ct->status = 0;
1da177e4 887 break;
1da177e4 888 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
c41f8cbd 889 zfcp_fsf_class_not_supp(req);
1da177e4 890 break;
1da177e4 891 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
892 switch (header->fsf_status_qual.word[0]){
893 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 894 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 895 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 896 break;
1da177e4
LT
897 }
898 break;
1da177e4 899 case FSF_ACCESS_DENIED:
1da177e4 900 break;
1da177e4 901 case FSF_PORT_BOXED:
4c571c65 902 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 903 break;
c41f8cbd 904 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 905 zfcp_erp_adapter_reopen(adapter, 0, "fsscth1", req);
dceab655 906 /* fall through */
c41f8cbd 907 case FSF_GENERIC_COMMAND_REJECTED:
1da177e4 908 case FSF_PAYLOAD_SIZE_MISMATCH:
1da177e4 909 case FSF_REQUEST_SIZE_TOO_LARGE:
1da177e4 910 case FSF_RESPONSE_SIZE_TOO_LARGE:
1da177e4 911 case FSF_SBAL_MISMATCH:
c41f8cbd 912 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
913 break;
914 }
915
916skip_fsfstatus:
7c7dc196
CS
917 if (ct->handler)
918 ct->handler(ct->handler_data);
c41f8cbd 919}
1da177e4 920
1674b405
CS
921static void zfcp_fsf_setup_ct_els_unchained(struct zfcp_qdio *qdio,
922 struct zfcp_qdio_req *q_req,
426f6059
CS
923 struct scatterlist *sg_req,
924 struct scatterlist *sg_resp)
925{
1674b405
CS
926 zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_req), sg_req->length);
927 zfcp_qdio_fill_next(qdio, q_req, sg_virt(sg_resp), sg_resp->length);
928 zfcp_qdio_set_sbale_last(qdio, q_req);
426f6059
CS
929}
930
39eb7e9a
CS
931static int zfcp_fsf_setup_ct_els_sbals(struct zfcp_fsf_req *req,
932 struct scatterlist *sg_req,
01b04759 933 struct scatterlist *sg_resp)
c41f8cbd 934{
42428f74 935 struct zfcp_adapter *adapter = req->adapter;
42428f74 936 u32 feat = adapter->adapter_features;
c41f8cbd
SS
937 int bytes;
938
39eb7e9a 939 if (!(feat & FSF_FEATURE_ELS_CT_CHAINED_SBALS)) {
1674b405
CS
940 if (!zfcp_qdio_sg_one_sbale(sg_req) ||
941 !zfcp_qdio_sg_one_sbale(sg_resp))
39eb7e9a
CS
942 return -EOPNOTSUPP;
943
1674b405
CS
944 zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
945 sg_req, sg_resp);
426f6059
CS
946 return 0;
947 }
948
949 /* use single, unchained SBAL if it can hold the request */
30b6777b 950 if (zfcp_qdio_sg_one_sbale(sg_req) && zfcp_qdio_sg_one_sbale(sg_resp)) {
1674b405
CS
951 zfcp_fsf_setup_ct_els_unchained(adapter->qdio, &req->qdio_req,
952 sg_req, sg_resp);
39eb7e9a
CS
953 return 0;
954 }
955
01b04759 956 bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->qdio_req, sg_req);
c41f8cbd 957 if (bytes <= 0)
9072df4d 958 return -EIO;
ef3eb71d 959 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
c41f8cbd 960 req->qtcb->bottom.support.req_buf_length = bytes;
1674b405 961 zfcp_qdio_skip_to_last_sbale(&req->qdio_req);
c41f8cbd 962
34c2b712 963 bytes = zfcp_qdio_sbals_from_sg(adapter->qdio, &req->qdio_req,
01b04759 964 sg_resp);
b1a58985 965 req->qtcb->bottom.support.resp_buf_length = bytes;
c41f8cbd 966 if (bytes <= 0)
9072df4d 967 return -EIO;
ef3eb71d 968 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
98fc4d5c 969
b1a58985
CS
970 return 0;
971}
972
973static int zfcp_fsf_setup_ct_els(struct zfcp_fsf_req *req,
974 struct scatterlist *sg_req,
975 struct scatterlist *sg_resp,
01b04759 976 unsigned int timeout)
b1a58985
CS
977{
978 int ret;
979
01b04759 980 ret = zfcp_fsf_setup_ct_els_sbals(req, sg_req, sg_resp);
b1a58985
CS
981 if (ret)
982 return ret;
983
98fc4d5c 984 /* common settings for ct/gs and els requests */
51375ee8
SS
985 if (timeout > 255)
986 timeout = 255; /* max value accepted by hardware */
98fc4d5c 987 req->qtcb->bottom.support.service_class = FSF_CLASS_3;
51375ee8
SS
988 req->qtcb->bottom.support.timeout = timeout;
989 zfcp_fsf_start_timer(req, (timeout + 10) * HZ);
c41f8cbd
SS
990
991 return 0;
1da177e4
LT
992}
993
994/**
c41f8cbd
SS
995 * zfcp_fsf_send_ct - initiate a Generic Service request (FC-GS)
996 * @ct: pointer to struct zfcp_send_ct with data for request
997 * @pool: if non-null this mempool is used to allocate struct zfcp_fsf_req
1da177e4 998 */
7c7dc196 999int zfcp_fsf_send_ct(struct zfcp_fc_wka_port *wka_port,
51375ee8
SS
1000 struct zfcp_fsf_ct_els *ct, mempool_t *pool,
1001 unsigned int timeout)
1da177e4 1002{
564e1c86 1003 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
c41f8cbd
SS
1004 struct zfcp_fsf_req *req;
1005 int ret = -EIO;
1da177e4 1006
44a24cb3 1007 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1008 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1009 goto out;
1da177e4 1010
1674b405
CS
1011 req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_GENERIC,
1012 SBAL_FLAGS0_TYPE_WRITE_READ, pool);
09a46c6e 1013
025270f0 1014 if (IS_ERR(req)) {
c41f8cbd
SS
1015 ret = PTR_ERR(req);
1016 goto out;
3f0ca62a
CS
1017 }
1018
09a46c6e 1019 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
01b04759 1020 ret = zfcp_fsf_setup_ct_els(req, ct->req, ct->resp, timeout);
553448f6 1021 if (ret)
1da177e4 1022 goto failed_send;
1da177e4 1023
c41f8cbd 1024 req->handler = zfcp_fsf_send_ct_handler;
5ab944f9 1025 req->qtcb->header.port_handle = wka_port->handle;
c41f8cbd
SS
1026 req->data = ct;
1027
7c7dc196 1028 zfcp_dbf_san_ct_request(req, wka_port->d_id);
1da177e4 1029
c41f8cbd
SS
1030 ret = zfcp_fsf_req_send(req);
1031 if (ret)
1032 goto failed_send;
1da177e4 1033
c41f8cbd 1034 goto out;
1da177e4 1035
c41f8cbd
SS
1036failed_send:
1037 zfcp_fsf_req_free(req);
c41f8cbd 1038out:
44a24cb3 1039 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 1040 return ret;
1da177e4
LT
1041}
1042
c41f8cbd 1043static void zfcp_fsf_send_els_handler(struct zfcp_fsf_req *req)
1da177e4 1044{
7c7dc196 1045 struct zfcp_fsf_ct_els *send_els = req->data;
c41f8cbd
SS
1046 struct zfcp_port *port = send_els->port;
1047 struct fsf_qtcb_header *header = &req->qtcb->header;
1da177e4 1048
c41f8cbd 1049 send_els->status = -EINVAL;
1da177e4 1050
c41f8cbd 1051 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1da177e4
LT
1052 goto skip_fsfstatus;
1053
1054 switch (header->fsf_status) {
1da177e4 1055 case FSF_GOOD:
5771710b 1056 zfcp_dbf_san_els_response(req);
c41f8cbd 1057 send_els->status = 0;
1da177e4 1058 break;
1da177e4 1059 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
c41f8cbd 1060 zfcp_fsf_class_not_supp(req);
1da177e4 1061 break;
1da177e4 1062 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1063 switch (header->fsf_status_qual.word[0]){
1064 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 1065 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1da177e4 1066 case FSF_SQ_RETRY_IF_POSSIBLE:
c41f8cbd 1067 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1068 break;
1da177e4
LT
1069 }
1070 break;
1da177e4 1071 case FSF_ELS_COMMAND_REJECTED:
1da177e4 1072 case FSF_PAYLOAD_SIZE_MISMATCH:
1da177e4 1073 case FSF_REQUEST_SIZE_TOO_LARGE:
1da177e4 1074 case FSF_RESPONSE_SIZE_TOO_LARGE:
1da177e4 1075 break;
1da177e4 1076 case FSF_ACCESS_DENIED:
a1ca4831
CS
1077 if (port) {
1078 zfcp_cfdc_port_denied(port, &header->fsf_status_qual);
1079 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1080 }
1da177e4 1081 break;
c41f8cbd
SS
1082 case FSF_SBAL_MISMATCH:
1083 /* should never occure, avoided in zfcp_fsf_send_els */
1084 /* fall through */
1da177e4 1085 default:
c41f8cbd 1086 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1087 break;
1088 }
1da177e4 1089skip_fsfstatus:
aa551daf 1090 if (send_els->handler)
1da177e4 1091 send_els->handler(send_els->handler_data);
c41f8cbd 1092}
1da177e4 1093
c41f8cbd
SS
1094/**
1095 * zfcp_fsf_send_els - initiate an ELS command (FC-FS)
1096 * @els: pointer to struct zfcp_send_els with data for the command
1097 */
7c7dc196 1098int zfcp_fsf_send_els(struct zfcp_adapter *adapter, u32 d_id,
51375ee8 1099 struct zfcp_fsf_ct_els *els, unsigned int timeout)
c41f8cbd
SS
1100{
1101 struct zfcp_fsf_req *req;
7c7dc196 1102 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
1103 int ret = -EIO;
1104
44a24cb3 1105 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1106 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1107 goto out;
09a46c6e 1108
1674b405
CS
1109 req = zfcp_fsf_req_create(qdio, FSF_QTCB_SEND_ELS,
1110 SBAL_FLAGS0_TYPE_WRITE_READ, NULL);
09a46c6e 1111
025270f0 1112 if (IS_ERR(req)) {
c41f8cbd
SS
1113 ret = PTR_ERR(req);
1114 goto out;
1115 }
1116
09a46c6e 1117 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
01b04759
SS
1118
1119 zfcp_qdio_sbal_limit(qdio, &req->qdio_req, 2);
1120
1121 ret = zfcp_fsf_setup_ct_els(req, els->req, els->resp, timeout);
44cc76f2 1122
c41f8cbd
SS
1123 if (ret)
1124 goto failed_send;
1125
7c7dc196 1126 hton24(req->qtcb->bottom.support.d_id, d_id);
c41f8cbd 1127 req->handler = zfcp_fsf_send_els_handler;
c41f8cbd
SS
1128 req->data = els;
1129
5771710b 1130 zfcp_dbf_san_els_request(req);
c41f8cbd 1131
c41f8cbd
SS
1132 ret = zfcp_fsf_req_send(req);
1133 if (ret)
1134 goto failed_send;
1135
1136 goto out;
1137
1138failed_send:
1139 zfcp_fsf_req_free(req);
1140out:
44a24cb3 1141 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 1142 return ret;
1da177e4
LT
1143}
1144
c41f8cbd 1145int zfcp_fsf_exchange_config_data(struct zfcp_erp_action *erp_action)
1da177e4 1146{
c41f8cbd 1147 struct zfcp_fsf_req *req;
564e1c86 1148 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1149 int retval = -EIO;
1150
44a24cb3 1151 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1152 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1153 goto out;
09a46c6e 1154
564e1c86 1155 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1674b405 1156 SBAL_FLAGS0_TYPE_READ,
564e1c86 1157 qdio->adapter->pool.erp_req);
09a46c6e 1158
025270f0 1159 if (IS_ERR(req)) {
c41f8cbd
SS
1160 retval = PTR_ERR(req);
1161 goto out;
1da177e4
LT
1162 }
1163
09a46c6e 1164 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1165 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1166
c41f8cbd 1167 req->qtcb->bottom.config.feature_selection =
aef4a983
MS
1168 FSF_FEATURE_CFDC |
1169 FSF_FEATURE_LUN_SHARING |
9eb69aff 1170 FSF_FEATURE_NOTIFICATION_LOST |
aef4a983 1171 FSF_FEATURE_UPDATE_ALERT;
c41f8cbd
SS
1172 req->erp_action = erp_action;
1173 req->handler = zfcp_fsf_exchange_config_data_handler;
e60a6d69 1174 erp_action->fsf_req_id = req->req_id;
1da177e4 1175
287ac01a 1176 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1177 retval = zfcp_fsf_req_send(req);
1da177e4 1178 if (retval) {
c41f8cbd 1179 zfcp_fsf_req_free(req);
e60a6d69 1180 erp_action->fsf_req_id = 0;
1da177e4 1181 }
c41f8cbd 1182out:
44a24cb3 1183 spin_unlock_irq(&qdio->req_q_lock);
52ef11a7
SS
1184 return retval;
1185}
1da177e4 1186
564e1c86 1187int zfcp_fsf_exchange_config_data_sync(struct zfcp_qdio *qdio,
c41f8cbd 1188 struct fsf_qtcb_bottom_config *data)
52ef11a7 1189{
c41f8cbd
SS
1190 struct zfcp_fsf_req *req = NULL;
1191 int retval = -EIO;
1192
44a24cb3 1193 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1194 if (zfcp_qdio_sbal_get(qdio))
ada81b74 1195 goto out_unlock;
c41f8cbd 1196
1674b405
CS
1197 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_CONFIG_DATA,
1198 SBAL_FLAGS0_TYPE_READ, NULL);
09a46c6e 1199
025270f0 1200 if (IS_ERR(req)) {
c41f8cbd 1201 retval = PTR_ERR(req);
ada81b74 1202 goto out_unlock;
52ef11a7
SS
1203 }
1204
1674b405 1205 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
c41f8cbd 1206 req->handler = zfcp_fsf_exchange_config_data_handler;
52ef11a7 1207
c41f8cbd 1208 req->qtcb->bottom.config.feature_selection =
52ef11a7
SS
1209 FSF_FEATURE_CFDC |
1210 FSF_FEATURE_LUN_SHARING |
1211 FSF_FEATURE_NOTIFICATION_LOST |
1212 FSF_FEATURE_UPDATE_ALERT;
1213
1214 if (data)
c41f8cbd 1215 req->data = data;
52ef11a7 1216
c41f8cbd
SS
1217 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1218 retval = zfcp_fsf_req_send(req);
44a24cb3 1219 spin_unlock_irq(&qdio->req_q_lock);
553448f6 1220 if (!retval)
058b8647 1221 wait_for_completion(&req->completion);
52ef11a7 1222
c41f8cbd 1223 zfcp_fsf_req_free(req);
ada81b74 1224 return retval;
52ef11a7 1225
ada81b74 1226out_unlock:
44a24cb3 1227 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1228 return retval;
1229}
1230
1da177e4
LT
1231/**
1232 * zfcp_fsf_exchange_port_data - request information about local port
aef4a983 1233 * @erp_action: ERP action for the adapter for which port data is requested
c41f8cbd 1234 * Returns: 0 on success, error otherwise
1da177e4 1235 */
c41f8cbd 1236int zfcp_fsf_exchange_port_data(struct zfcp_erp_action *erp_action)
1da177e4 1237{
564e1c86 1238 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd 1239 struct zfcp_fsf_req *req;
c41f8cbd 1240 int retval = -EIO;
1da177e4 1241
564e1c86 1242 if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
52ef11a7 1243 return -EOPNOTSUPP;
1da177e4 1244
44a24cb3 1245 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1246 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 1247 goto out;
09a46c6e 1248
564e1c86 1249 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1674b405 1250 SBAL_FLAGS0_TYPE_READ,
564e1c86 1251 qdio->adapter->pool.erp_req);
09a46c6e 1252
025270f0 1253 if (IS_ERR(req)) {
c41f8cbd
SS
1254 retval = PTR_ERR(req);
1255 goto out;
aef4a983
MS
1256 }
1257
09a46c6e 1258 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1259 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1260
c41f8cbd
SS
1261 req->handler = zfcp_fsf_exchange_port_data_handler;
1262 req->erp_action = erp_action;
e60a6d69 1263 erp_action->fsf_req_id = req->req_id;
52ef11a7 1264
287ac01a 1265 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1266 retval = zfcp_fsf_req_send(req);
1da177e4 1267 if (retval) {
c41f8cbd 1268 zfcp_fsf_req_free(req);
e60a6d69 1269 erp_action->fsf_req_id = 0;
52ef11a7 1270 }
c41f8cbd 1271out:
44a24cb3 1272 spin_unlock_irq(&qdio->req_q_lock);
52ef11a7
SS
1273 return retval;
1274}
1275
52ef11a7
SS
1276/**
1277 * zfcp_fsf_exchange_port_data_sync - request information about local port
564e1c86 1278 * @qdio: pointer to struct zfcp_qdio
c41f8cbd
SS
1279 * @data: pointer to struct fsf_qtcb_bottom_port
1280 * Returns: 0 on success, error otherwise
52ef11a7 1281 */
564e1c86 1282int zfcp_fsf_exchange_port_data_sync(struct zfcp_qdio *qdio,
c41f8cbd 1283 struct fsf_qtcb_bottom_port *data)
52ef11a7 1284{
c41f8cbd
SS
1285 struct zfcp_fsf_req *req = NULL;
1286 int retval = -EIO;
52ef11a7 1287
564e1c86 1288 if (!(qdio->adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT))
52ef11a7 1289 return -EOPNOTSUPP;
52ef11a7 1290
44a24cb3 1291 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1292 if (zfcp_qdio_sbal_get(qdio))
ada81b74 1293 goto out_unlock;
c41f8cbd 1294
1674b405
CS
1295 req = zfcp_fsf_req_create(qdio, FSF_QTCB_EXCHANGE_PORT_DATA,
1296 SBAL_FLAGS0_TYPE_READ, NULL);
09a46c6e 1297
025270f0 1298 if (IS_ERR(req)) {
c41f8cbd 1299 retval = PTR_ERR(req);
ada81b74 1300 goto out_unlock;
1da177e4
LT
1301 }
1302
52ef11a7 1303 if (data)
c41f8cbd 1304 req->data = data;
52ef11a7 1305
1674b405 1306 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
52ef11a7 1307
c41f8cbd
SS
1308 req->handler = zfcp_fsf_exchange_port_data_handler;
1309 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1310 retval = zfcp_fsf_req_send(req);
44a24cb3 1311 spin_unlock_irq(&qdio->req_q_lock);
ada81b74 1312
553448f6 1313 if (!retval)
058b8647
SS
1314 wait_for_completion(&req->completion);
1315
c41f8cbd 1316 zfcp_fsf_req_free(req);
1da177e4 1317
1da177e4 1318 return retval;
ada81b74
CS
1319
1320out_unlock:
44a24cb3 1321 spin_unlock_irq(&qdio->req_q_lock);
ada81b74 1322 return retval;
1da177e4
LT
1323}
1324
c41f8cbd 1325static void zfcp_fsf_open_port_handler(struct zfcp_fsf_req *req)
1da177e4 1326{
c41f8cbd
SS
1327 struct zfcp_port *port = req->data;
1328 struct fsf_qtcb_header *header = &req->qtcb->header;
9d05ce2c 1329 struct fc_els_flogi *plogi;
1da177e4 1330
c41f8cbd 1331 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
a17c5855 1332 goto out;
1da177e4 1333
1da177e4 1334 switch (header->fsf_status) {
1da177e4 1335 case FSF_PORT_ALREADY_OPEN:
1da177e4 1336 break;
1da177e4 1337 case FSF_ACCESS_DENIED:
a1ca4831
CS
1338 zfcp_cfdc_port_denied(port, &header->fsf_status_qual);
1339 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1340 break;
1da177e4 1341 case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
c41f8cbd 1342 dev_warn(&req->adapter->ccw_device->dev,
ff3b24fa 1343 "Not enough FCP adapter resources to open "
7ba58c9c
SS
1344 "remote port 0x%016Lx\n",
1345 (unsigned long long)port->wwpn);
edaed859
SS
1346 zfcp_erp_set_port_status(port,
1347 ZFCP_STATUS_COMMON_ERP_FAILED);
c41f8cbd 1348 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1349 break;
1da177e4 1350 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1351 switch (header->fsf_status_qual.word[0]) {
1352 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
1da177e4 1353 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
1da177e4 1354 case FSF_SQ_NO_RETRY_POSSIBLE:
c41f8cbd 1355 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1356 break;
1357 }
1358 break;
1da177e4 1359 case FSF_GOOD:
1da177e4 1360 port->handle = header->port_handle;
1da177e4
LT
1361 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN |
1362 ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
d736a27b
AH
1363 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
1364 ZFCP_STATUS_COMMON_ACCESS_BOXED,
1365 &port->status);
1da177e4
LT
1366 /* check whether D_ID has changed during open */
1367 /*
1368 * FIXME: This check is not airtight, as the FCP channel does
1369 * not monitor closures of target port connections caused on
1370 * the remote side. Thus, they might miss out on invalidating
1371 * locally cached WWPNs (and other N_Port parameters) of gone
1372 * target ports. So, our heroic attempt to make things safe
1373 * could be undermined by 'open port' response data tagged with
1374 * obsolete WWPNs. Another reason to monitor potential
1375 * connection closures ourself at least (by interpreting
1376 * incoming ELS' and unsolicited status). It just crosses my
1377 * mind that one should be able to cross-check by means of
1378 * another GID_PN straight after a port has been opened.
1379 * Alternately, an ADISC/PDISC ELS should suffice, as well.
1380 */
9d05ce2c 1381 plogi = (struct fc_els_flogi *) req->qtcb->bottom.support.els;
39eb7e9a 1382 if (req->qtcb->bottom.support.els1_length >=
9d05ce2c 1383 FSF_PLOGI_MIN_LEN)
c41f8cbd 1384 zfcp_fc_plogi_evaluate(port, plogi);
1da177e4 1385 break;
1da177e4 1386 case FSF_UNKNOWN_OP_SUBTYPE:
c41f8cbd 1387 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1388 break;
1389 }
a17c5855
MP
1390
1391out:
615f59e0 1392 put_device(&port->dev);
1da177e4
LT
1393}
1394
c41f8cbd
SS
1395/**
1396 * zfcp_fsf_open_port - create and send open port request
1397 * @erp_action: pointer to struct zfcp_erp_action
1398 * Returns: 0 on success, error otherwise
1da177e4 1399 */
c41f8cbd 1400int zfcp_fsf_open_port(struct zfcp_erp_action *erp_action)
1da177e4 1401{
564e1c86 1402 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
a17c5855 1403 struct zfcp_port *port = erp_action->port;
564e1c86 1404 struct zfcp_fsf_req *req;
c41f8cbd
SS
1405 int retval = -EIO;
1406
44a24cb3 1407 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1408 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
1409 goto out;
1410
564e1c86 1411 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1674b405 1412 SBAL_FLAGS0_TYPE_READ,
564e1c86 1413 qdio->adapter->pool.erp_req);
09a46c6e 1414
025270f0 1415 if (IS_ERR(req)) {
c41f8cbd 1416 retval = PTR_ERR(req);
1da177e4 1417 goto out;
c41f8cbd 1418 }
1da177e4 1419
09a46c6e 1420 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1421 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1422
c41f8cbd 1423 req->handler = zfcp_fsf_open_port_handler;
800c0cad 1424 hton24(req->qtcb->bottom.support.d_id, port->d_id);
a17c5855 1425 req->data = port;
c41f8cbd 1426 req->erp_action = erp_action;
e60a6d69 1427 erp_action->fsf_req_id = req->req_id;
615f59e0 1428 get_device(&port->dev);
c41f8cbd 1429
287ac01a 1430 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1431 retval = zfcp_fsf_req_send(req);
1da177e4 1432 if (retval) {
c41f8cbd 1433 zfcp_fsf_req_free(req);
e60a6d69 1434 erp_action->fsf_req_id = 0;
615f59e0 1435 put_device(&port->dev);
1da177e4 1436 }
c41f8cbd 1437out:
44a24cb3 1438 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1439 return retval;
1440}
1441
c41f8cbd 1442static void zfcp_fsf_close_port_handler(struct zfcp_fsf_req *req)
1da177e4 1443{
c41f8cbd 1444 struct zfcp_port *port = req->data;
1da177e4 1445
c41f8cbd 1446 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1447 return;
1da177e4 1448
c41f8cbd 1449 switch (req->qtcb->header.fsf_status) {
1da177e4 1450 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 1451 zfcp_erp_adapter_reopen(port->adapter, 0, "fscph_1", req);
c41f8cbd 1452 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1453 break;
1da177e4 1454 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4 1455 break;
1da177e4 1456 case FSF_GOOD:
edaed859 1457 zfcp_erp_clear_port_status(port, ZFCP_STATUS_COMMON_OPEN);
1da177e4 1458 break;
1da177e4 1459 }
1da177e4
LT
1460}
1461
c41f8cbd
SS
1462/**
1463 * zfcp_fsf_close_port - create and send close port request
1464 * @erp_action: pointer to struct zfcp_erp_action
1465 * Returns: 0 on success, error otherwise
1da177e4 1466 */
c41f8cbd 1467int zfcp_fsf_close_port(struct zfcp_erp_action *erp_action)
1da177e4 1468{
564e1c86 1469 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1470 struct zfcp_fsf_req *req;
1471 int retval = -EIO;
1472
44a24cb3 1473 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1474 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd
SS
1475 goto out;
1476
564e1c86 1477 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1674b405 1478 SBAL_FLAGS0_TYPE_READ,
564e1c86 1479 qdio->adapter->pool.erp_req);
09a46c6e 1480
025270f0 1481 if (IS_ERR(req)) {
c41f8cbd 1482 retval = PTR_ERR(req);
1da177e4 1483 goto out;
c41f8cbd 1484 }
1da177e4 1485
09a46c6e 1486 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1487 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1488
c41f8cbd
SS
1489 req->handler = zfcp_fsf_close_port_handler;
1490 req->data = erp_action->port;
1491 req->erp_action = erp_action;
1492 req->qtcb->header.port_handle = erp_action->port->handle;
e60a6d69 1493 erp_action->fsf_req_id = req->req_id;
c41f8cbd 1494
287ac01a 1495 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1496 retval = zfcp_fsf_req_send(req);
1da177e4 1497 if (retval) {
c41f8cbd 1498 zfcp_fsf_req_free(req);
e60a6d69 1499 erp_action->fsf_req_id = 0;
1da177e4 1500 }
c41f8cbd 1501out:
44a24cb3 1502 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1503 return retval;
1504}
1505
5ab944f9
SS
1506static void zfcp_fsf_open_wka_port_handler(struct zfcp_fsf_req *req)
1507{
bd0072ec 1508 struct zfcp_fc_wka_port *wka_port = req->data;
5ab944f9
SS
1509 struct fsf_qtcb_header *header = &req->qtcb->header;
1510
1511 if (req->status & ZFCP_STATUS_FSFREQ_ERROR) {
bd0072ec 1512 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
1513 goto out;
1514 }
1515
1516 switch (header->fsf_status) {
1517 case FSF_MAXIMUM_NUMBER_OF_PORTS_EXCEEDED:
1518 dev_warn(&req->adapter->ccw_device->dev,
1519 "Opening WKA port 0x%x failed\n", wka_port->d_id);
dceab655 1520 /* fall through */
5ab944f9
SS
1521 case FSF_ADAPTER_STATUS_AVAILABLE:
1522 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
dceab655 1523 /* fall through */
5ab944f9 1524 case FSF_ACCESS_DENIED:
bd0072ec 1525 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9 1526 break;
5ab944f9
SS
1527 case FSF_GOOD:
1528 wka_port->handle = header->port_handle;
27f492cc
SS
1529 /* fall through */
1530 case FSF_PORT_ALREADY_OPEN:
bd0072ec 1531 wka_port->status = ZFCP_FC_WKA_PORT_ONLINE;
5ab944f9
SS
1532 }
1533out:
1534 wake_up(&wka_port->completion_wq);
1535}
1536
1537/**
1538 * zfcp_fsf_open_wka_port - create and send open wka-port request
bd0072ec 1539 * @wka_port: pointer to struct zfcp_fc_wka_port
5ab944f9
SS
1540 * Returns: 0 on success, error otherwise
1541 */
bd0072ec 1542int zfcp_fsf_open_wka_port(struct zfcp_fc_wka_port *wka_port)
5ab944f9 1543{
564e1c86 1544 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
5ab944f9
SS
1545 struct zfcp_fsf_req *req;
1546 int retval = -EIO;
1547
44a24cb3 1548 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1549 if (zfcp_qdio_sbal_get(qdio))
5ab944f9
SS
1550 goto out;
1551
564e1c86 1552 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_PORT_WITH_DID,
1674b405 1553 SBAL_FLAGS0_TYPE_READ,
564e1c86 1554 qdio->adapter->pool.erp_req);
09a46c6e 1555
5ab944f9
SS
1556 if (unlikely(IS_ERR(req))) {
1557 retval = PTR_ERR(req);
1558 goto out;
1559 }
1560
09a46c6e 1561 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1562 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
5ab944f9
SS
1563
1564 req->handler = zfcp_fsf_open_wka_port_handler;
800c0cad 1565 hton24(req->qtcb->bottom.support.d_id, wka_port->d_id);
5ab944f9
SS
1566 req->data = wka_port;
1567
1568 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1569 retval = zfcp_fsf_req_send(req);
1570 if (retval)
1571 zfcp_fsf_req_free(req);
1572out:
44a24cb3 1573 spin_unlock_irq(&qdio->req_q_lock);
5ab944f9
SS
1574 return retval;
1575}
1576
1577static void zfcp_fsf_close_wka_port_handler(struct zfcp_fsf_req *req)
1578{
bd0072ec 1579 struct zfcp_fc_wka_port *wka_port = req->data;
5ab944f9
SS
1580
1581 if (req->qtcb->header.fsf_status == FSF_PORT_HANDLE_NOT_VALID) {
1582 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
5ffd51a5 1583 zfcp_erp_adapter_reopen(wka_port->adapter, 0, "fscwph1", req);
5ab944f9
SS
1584 }
1585
bd0072ec 1586 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
5ab944f9
SS
1587 wake_up(&wka_port->completion_wq);
1588}
1589
1590/**
1591 * zfcp_fsf_close_wka_port - create and send close wka port request
bd0072ec 1592 * @wka_port: WKA port to open
5ab944f9
SS
1593 * Returns: 0 on success, error otherwise
1594 */
bd0072ec 1595int zfcp_fsf_close_wka_port(struct zfcp_fc_wka_port *wka_port)
5ab944f9 1596{
564e1c86 1597 struct zfcp_qdio *qdio = wka_port->adapter->qdio;
5ab944f9
SS
1598 struct zfcp_fsf_req *req;
1599 int retval = -EIO;
1600
44a24cb3 1601 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1602 if (zfcp_qdio_sbal_get(qdio))
5ab944f9
SS
1603 goto out;
1604
564e1c86 1605 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PORT,
1674b405 1606 SBAL_FLAGS0_TYPE_READ,
564e1c86 1607 qdio->adapter->pool.erp_req);
09a46c6e 1608
5ab944f9
SS
1609 if (unlikely(IS_ERR(req))) {
1610 retval = PTR_ERR(req);
1611 goto out;
1612 }
1613
09a46c6e 1614 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1615 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
5ab944f9
SS
1616
1617 req->handler = zfcp_fsf_close_wka_port_handler;
1618 req->data = wka_port;
1619 req->qtcb->header.port_handle = wka_port->handle;
1620
1621 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
1622 retval = zfcp_fsf_req_send(req);
1623 if (retval)
1624 zfcp_fsf_req_free(req);
1625out:
44a24cb3 1626 spin_unlock_irq(&qdio->req_q_lock);
5ab944f9
SS
1627 return retval;
1628}
1629
c41f8cbd 1630static void zfcp_fsf_close_physical_port_handler(struct zfcp_fsf_req *req)
1da177e4 1631{
c41f8cbd
SS
1632 struct zfcp_port *port = req->data;
1633 struct fsf_qtcb_header *header = &req->qtcb->header;
b62a8d9b 1634 struct scsi_device *sdev;
1da177e4 1635
c41f8cbd 1636 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
a5b11dda 1637 return;
1da177e4 1638
1da177e4 1639 switch (header->fsf_status) {
1da177e4 1640 case FSF_PORT_HANDLE_NOT_VALID:
5ffd51a5 1641 zfcp_erp_adapter_reopen(port->adapter, 0, "fscpph1", req);
c41f8cbd 1642 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1643 break;
1da177e4 1644 case FSF_ACCESS_DENIED:
a1ca4831 1645 zfcp_cfdc_port_denied(port, &header->fsf_status_qual);
1da177e4 1646 break;
1da177e4 1647 case FSF_PORT_BOXED:
5c815d15
CS
1648 /* can't use generic zfcp_erp_modify_port_status because
1649 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port */
1650 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
b62a8d9b
CS
1651 shost_for_each_device(sdev, port->adapter->scsi_host)
1652 if (sdev_to_zfcp(sdev)->port == port)
1653 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1654 &sdev_to_zfcp(sdev)->status);
edaed859
SS
1655 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ACCESS_BOXED);
1656 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
1657 "fscpph2", req);
4c571c65 1658 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1659 break;
1da177e4 1660 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1661 switch (header->fsf_status_qual.word[0]) {
1662 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
c41f8cbd 1663 /* fall through */
1da177e4 1664 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1665 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1666 break;
1da177e4
LT
1667 }
1668 break;
1da177e4 1669 case FSF_GOOD:
1da177e4
LT
1670 /* can't use generic zfcp_erp_modify_port_status because
1671 * ZFCP_STATUS_COMMON_OPEN must not be reset for the port
1672 */
1673 atomic_clear_mask(ZFCP_STATUS_PORT_PHYS_OPEN, &port->status);
b62a8d9b
CS
1674 shost_for_each_device(sdev, port->adapter->scsi_host)
1675 if (sdev_to_zfcp(sdev)->port == port)
1676 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN,
1677 &sdev_to_zfcp(sdev)->status);
1da177e4 1678 break;
1da177e4 1679 }
1da177e4
LT
1680}
1681
c41f8cbd
SS
1682/**
1683 * zfcp_fsf_close_physical_port - close physical port
1684 * @erp_action: pointer to struct zfcp_erp_action
1685 * Returns: 0 on success
1da177e4 1686 */
c41f8cbd 1687int zfcp_fsf_close_physical_port(struct zfcp_erp_action *erp_action)
1da177e4 1688{
564e1c86 1689 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
c41f8cbd
SS
1690 struct zfcp_fsf_req *req;
1691 int retval = -EIO;
1692
44a24cb3 1693 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1694 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1695 goto out;
1da177e4 1696
564e1c86 1697 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_PHYSICAL_PORT,
1674b405 1698 SBAL_FLAGS0_TYPE_READ,
564e1c86 1699 qdio->adapter->pool.erp_req);
09a46c6e 1700
025270f0 1701 if (IS_ERR(req)) {
c41f8cbd
SS
1702 retval = PTR_ERR(req);
1703 goto out;
1704 }
1da177e4 1705
09a46c6e 1706 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1707 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1708
c41f8cbd
SS
1709 req->data = erp_action->port;
1710 req->qtcb->header.port_handle = erp_action->port->handle;
1711 req->erp_action = erp_action;
1712 req->handler = zfcp_fsf_close_physical_port_handler;
e60a6d69 1713 erp_action->fsf_req_id = req->req_id;
c41f8cbd 1714
287ac01a 1715 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1716 retval = zfcp_fsf_req_send(req);
1da177e4 1717 if (retval) {
c41f8cbd 1718 zfcp_fsf_req_free(req);
e60a6d69 1719 erp_action->fsf_req_id = 0;
1da177e4 1720 }
c41f8cbd 1721out:
44a24cb3 1722 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1723 return retval;
1724}
1725
b62a8d9b 1726static void zfcp_fsf_open_lun_handler(struct zfcp_fsf_req *req)
1da177e4 1727{
c41f8cbd 1728 struct zfcp_adapter *adapter = req->adapter;
b62a8d9b
CS
1729 struct scsi_device *sdev = req->data;
1730 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
c41f8cbd
SS
1731 struct fsf_qtcb_header *header = &req->qtcb->header;
1732 struct fsf_qtcb_bottom_support *bottom = &req->qtcb->bottom.support;
1da177e4 1733
c41f8cbd 1734 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1735 return;
1da177e4 1736
1da177e4 1737 atomic_clear_mask(ZFCP_STATUS_COMMON_ACCESS_DENIED |
b64ddf96 1738 ZFCP_STATUS_COMMON_ACCESS_BOXED |
b62a8d9b
CS
1739 ZFCP_STATUS_LUN_SHARED |
1740 ZFCP_STATUS_LUN_READONLY,
1741 &zfcp_sdev->status);
1da177e4 1742
1da177e4
LT
1743 switch (header->fsf_status) {
1744
1745 case FSF_PORT_HANDLE_NOT_VALID:
b62a8d9b 1746 zfcp_erp_adapter_reopen(adapter, 0, "fsouh_1", req);
c41f8cbd 1747 /* fall through */
1da177e4 1748 case FSF_LUN_ALREADY_OPEN:
1da177e4 1749 break;
1da177e4 1750 case FSF_ACCESS_DENIED:
a1ca4831
CS
1751 zfcp_cfdc_lun_denied(sdev, &header->fsf_status_qual);
1752 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1753 break;
1da177e4 1754 case FSF_PORT_BOXED:
edaed859
SS
1755 zfcp_erp_set_port_status(zfcp_sdev->port,
1756 ZFCP_STATUS_COMMON_ACCESS_BOXED);
1757 zfcp_erp_port_reopen(zfcp_sdev->port,
1758 ZFCP_STATUS_COMMON_ERP_FAILED, "fsouh_2",
1759 req);
4c571c65 1760 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1761 break;
1da177e4 1762 case FSF_LUN_SHARING_VIOLATION:
a1ca4831 1763 zfcp_cfdc_lun_shrng_vltn(sdev, &header->fsf_status_qual);
c41f8cbd 1764 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1765 break;
1da177e4 1766 case FSF_MAXIMUM_NUMBER_OF_LUNS_EXCEEDED:
c41f8cbd 1767 dev_warn(&adapter->ccw_device->dev,
ff3b24fa
CS
1768 "No handle is available for LUN "
1769 "0x%016Lx on port 0x%016Lx\n",
b62a8d9b
CS
1770 (unsigned long long)zfcp_scsi_dev_lun(sdev),
1771 (unsigned long long)zfcp_sdev->port->wwpn);
edaed859 1772 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ERP_FAILED);
c41f8cbd
SS
1773 /* fall through */
1774 case FSF_INVALID_COMMAND_OPTION:
1775 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1776 break;
1da177e4 1777 case FSF_ADAPTER_STATUS_AVAILABLE:
1da177e4
LT
1778 switch (header->fsf_status_qual.word[0]) {
1779 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
b62a8d9b 1780 zfcp_fc_test_link(zfcp_sdev->port);
c41f8cbd 1781 /* fall through */
1da177e4 1782 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1783 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1784 break;
1da177e4
LT
1785 }
1786 break;
1787
1da177e4 1788 case FSF_GOOD:
b62a8d9b
CS
1789 zfcp_sdev->lun_handle = header->lun_handle;
1790 atomic_set_mask(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
a1ca4831 1791 zfcp_cfdc_open_lun_eval(sdev, bottom);
1da177e4 1792 break;
1da177e4 1793 }
1da177e4
LT
1794}
1795
c41f8cbd 1796/**
b62a8d9b 1797 * zfcp_fsf_open_lun - open LUN
c41f8cbd
SS
1798 * @erp_action: pointer to struct zfcp_erp_action
1799 * Returns: 0 on success, error otherwise
1da177e4 1800 */
b62a8d9b 1801int zfcp_fsf_open_lun(struct zfcp_erp_action *erp_action)
1da177e4 1802{
c41f8cbd 1803 struct zfcp_adapter *adapter = erp_action->adapter;
564e1c86 1804 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
1805 struct zfcp_fsf_req *req;
1806 int retval = -EIO;
1807
44a24cb3 1808 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1809 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1810 goto out;
1da177e4 1811
564e1c86 1812 req = zfcp_fsf_req_create(qdio, FSF_QTCB_OPEN_LUN,
1674b405 1813 SBAL_FLAGS0_TYPE_READ,
a4623c46 1814 adapter->pool.erp_req);
09a46c6e 1815
025270f0 1816 if (IS_ERR(req)) {
c41f8cbd
SS
1817 retval = PTR_ERR(req);
1818 goto out;
1819 }
1820
09a46c6e 1821 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1822 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1823
c41f8cbd 1824 req->qtcb->header.port_handle = erp_action->port->handle;
b62a8d9b
CS
1825 req->qtcb->bottom.support.fcp_lun = zfcp_scsi_dev_lun(erp_action->sdev);
1826 req->handler = zfcp_fsf_open_lun_handler;
1827 req->data = erp_action->sdev;
c41f8cbd 1828 req->erp_action = erp_action;
e60a6d69 1829 erp_action->fsf_req_id = req->req_id;
c41f8cbd
SS
1830
1831 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE))
1832 req->qtcb->bottom.support.option = FSF_OPEN_LUN_SUPPRESS_BOXING;
1833
287ac01a 1834 zfcp_fsf_start_erp_timer(req);
c41f8cbd 1835 retval = zfcp_fsf_req_send(req);
1da177e4 1836 if (retval) {
c41f8cbd 1837 zfcp_fsf_req_free(req);
e60a6d69 1838 erp_action->fsf_req_id = 0;
1da177e4 1839 }
c41f8cbd 1840out:
44a24cb3 1841 spin_unlock_irq(&qdio->req_q_lock);
1da177e4
LT
1842 return retval;
1843}
1844
b62a8d9b 1845static void zfcp_fsf_close_lun_handler(struct zfcp_fsf_req *req)
1da177e4 1846{
b62a8d9b
CS
1847 struct scsi_device *sdev = req->data;
1848 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1da177e4 1849
c41f8cbd 1850 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
44cc76f2 1851 return;
1da177e4 1852
c41f8cbd 1853 switch (req->qtcb->header.fsf_status) {
1da177e4 1854 case FSF_PORT_HANDLE_NOT_VALID:
b62a8d9b
CS
1855 zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0, "fscuh_1",
1856 req);
c41f8cbd 1857 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1858 break;
1da177e4 1859 case FSF_LUN_HANDLE_NOT_VALID:
b62a8d9b 1860 zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fscuh_2", req);
c41f8cbd 1861 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1862 break;
1da177e4 1863 case FSF_PORT_BOXED:
edaed859
SS
1864 zfcp_erp_set_port_status(zfcp_sdev->port,
1865 ZFCP_STATUS_COMMON_ACCESS_BOXED);
1866 zfcp_erp_port_reopen(zfcp_sdev->port,
1867 ZFCP_STATUS_COMMON_ERP_FAILED, "fscuh_3",
1868 req);
4c571c65 1869 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 1870 break;
1da177e4 1871 case FSF_ADAPTER_STATUS_AVAILABLE:
c41f8cbd 1872 switch (req->qtcb->header.fsf_status_qual.word[0]) {
1da177e4 1873 case FSF_SQ_INVOKE_LINK_TEST_PROCEDURE:
b62a8d9b 1874 zfcp_fc_test_link(zfcp_sdev->port);
c41f8cbd 1875 /* fall through */
1da177e4 1876 case FSF_SQ_ULP_DEPENDENT_ERP_REQUIRED:
c41f8cbd 1877 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4
LT
1878 break;
1879 }
1880 break;
1da177e4 1881 case FSF_GOOD:
b62a8d9b 1882 atomic_clear_mask(ZFCP_STATUS_COMMON_OPEN, &zfcp_sdev->status);
1da177e4 1883 break;
1da177e4 1884 }
1da177e4
LT
1885}
1886
1887/**
b62a8d9b
CS
1888 * zfcp_fsf_close_LUN - close LUN
1889 * @erp_action: pointer to erp_action triggering the "close LUN"
c41f8cbd 1890 * Returns: 0 on success, error otherwise
1da177e4 1891 */
b62a8d9b 1892int zfcp_fsf_close_lun(struct zfcp_erp_action *erp_action)
1da177e4 1893{
564e1c86 1894 struct zfcp_qdio *qdio = erp_action->adapter->qdio;
b62a8d9b 1895 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(erp_action->sdev);
c41f8cbd
SS
1896 struct zfcp_fsf_req *req;
1897 int retval = -EIO;
1da177e4 1898
44a24cb3 1899 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 1900 if (zfcp_qdio_sbal_get(qdio))
1da177e4 1901 goto out;
09a46c6e 1902
564e1c86 1903 req = zfcp_fsf_req_create(qdio, FSF_QTCB_CLOSE_LUN,
1674b405 1904 SBAL_FLAGS0_TYPE_READ,
564e1c86 1905 qdio->adapter->pool.erp_req);
09a46c6e 1906
025270f0 1907 if (IS_ERR(req)) {
c41f8cbd
SS
1908 retval = PTR_ERR(req);
1909 goto out;
1910 }
1da177e4 1911
09a46c6e 1912 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
1674b405 1913 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 1914
c41f8cbd 1915 req->qtcb->header.port_handle = erp_action->port->handle;
b62a8d9b
CS
1916 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
1917 req->handler = zfcp_fsf_close_lun_handler;
1918 req->data = erp_action->sdev;
c41f8cbd 1919 req->erp_action = erp_action;
e60a6d69 1920 erp_action->fsf_req_id = req->req_id;
fdf23452 1921
287ac01a 1922 zfcp_fsf_start_erp_timer(req);
c41f8cbd
SS
1923 retval = zfcp_fsf_req_send(req);
1924 if (retval) {
1925 zfcp_fsf_req_free(req);
e60a6d69 1926 erp_action->fsf_req_id = 0;
c41f8cbd
SS
1927 }
1928out:
44a24cb3 1929 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd 1930 return retval;
1da177e4
LT
1931}
1932
c9615858
CS
1933static void zfcp_fsf_update_lat(struct fsf_latency_record *lat_rec, u32 lat)
1934{
1935 lat_rec->sum += lat;
c41f8cbd
SS
1936 lat_rec->min = min(lat_rec->min, lat);
1937 lat_rec->max = max(lat_rec->max, lat);
c9615858
CS
1938}
1939
d9742b42 1940static void zfcp_fsf_req_trace(struct zfcp_fsf_req *req, struct scsi_cmnd *scsi)
c9615858 1941{
d9742b42
CS
1942 struct fsf_qual_latency_info *lat_in;
1943 struct latency_cont *lat = NULL;
b62a8d9b 1944 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scsi->device);
d9742b42
CS
1945 struct zfcp_blk_drv_data blktrc;
1946 int ticks = req->adapter->timer_ticks;
c9615858 1947
d9742b42 1948 lat_in = &req->qtcb->prefix.prot_status_qual.latency_info;
c9615858 1949
d9742b42
CS
1950 blktrc.flags = 0;
1951 blktrc.magic = ZFCP_BLK_DRV_DATA_MAGIC;
1952 if (req->status & ZFCP_STATUS_FSFREQ_ERROR)
1953 blktrc.flags |= ZFCP_BLK_REQ_ERROR;
706eca49 1954 blktrc.inb_usage = 0;
34c2b712 1955 blktrc.outb_usage = req->qdio_req.qdio_outb_usage;
d9742b42 1956
5bbf297c
CS
1957 if (req->adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA &&
1958 !(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
d9742b42
CS
1959 blktrc.flags |= ZFCP_BLK_LAT_VALID;
1960 blktrc.channel_lat = lat_in->channel_lat * ticks;
1961 blktrc.fabric_lat = lat_in->fabric_lat * ticks;
1962
1963 switch (req->qtcb->bottom.io.data_direction) {
ef3eb71d
FB
1964 case FSF_DATADIR_DIF_READ_STRIP:
1965 case FSF_DATADIR_DIF_READ_CONVERT:
d9742b42 1966 case FSF_DATADIR_READ:
b62a8d9b 1967 lat = &zfcp_sdev->latencies.read;
d9742b42 1968 break;
ef3eb71d
FB
1969 case FSF_DATADIR_DIF_WRITE_INSERT:
1970 case FSF_DATADIR_DIF_WRITE_CONVERT:
d9742b42 1971 case FSF_DATADIR_WRITE:
b62a8d9b 1972 lat = &zfcp_sdev->latencies.write;
d9742b42
CS
1973 break;
1974 case FSF_DATADIR_CMND:
b62a8d9b 1975 lat = &zfcp_sdev->latencies.cmd;
d9742b42
CS
1976 break;
1977 }
1da177e4 1978
d9742b42 1979 if (lat) {
b62a8d9b 1980 spin_lock(&zfcp_sdev->latencies.lock);
d9742b42
CS
1981 zfcp_fsf_update_lat(&lat->channel, lat_in->channel_lat);
1982 zfcp_fsf_update_lat(&lat->fabric, lat_in->fabric_lat);
1983 lat->counter++;
b62a8d9b 1984 spin_unlock(&zfcp_sdev->latencies.lock);
d9742b42 1985 }
0997f1c5 1986 }
0997f1c5 1987
d9742b42
CS
1988 blk_add_driver_data(scsi->request->q, scsi->request, &blktrc,
1989 sizeof(blktrc));
0997f1c5 1990}
0997f1c5 1991
c61b536c 1992static void zfcp_fsf_fcp_handler_common(struct zfcp_fsf_req *req)
c41f8cbd 1993{
b62a8d9b
CS
1994 struct scsi_cmnd *scmnd = req->data;
1995 struct scsi_device *sdev = scmnd->device;
1996 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
c41f8cbd
SS
1997 struct fsf_qtcb_header *header = &req->qtcb->header;
1998
c41f8cbd 1999 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR))
c61b536c 2000 return;
1da177e4 2001
c41f8cbd
SS
2002 switch (header->fsf_status) {
2003 case FSF_HANDLE_MISMATCH:
2004 case FSF_PORT_HANDLE_NOT_VALID:
b62a8d9b
CS
2005 zfcp_erp_adapter_reopen(zfcp_sdev->port->adapter, 0, "fssfch1",
2006 req);
c41f8cbd
SS
2007 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2008 break;
2009 case FSF_FCPLUN_NOT_VALID:
2010 case FSF_LUN_HANDLE_NOT_VALID:
b62a8d9b 2011 zfcp_erp_port_reopen(zfcp_sdev->port, 0, "fssfch2", req);
c41f8cbd 2012 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 2013 break;
c41f8cbd
SS
2014 case FSF_SERVICE_CLASS_NOT_SUPPORTED:
2015 zfcp_fsf_class_not_supp(req);
1da177e4 2016 break;
c41f8cbd 2017 case FSF_ACCESS_DENIED:
a1ca4831
CS
2018 zfcp_cfdc_lun_denied(sdev, &header->fsf_status_qual);
2019 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
2020 break;
2021 case FSF_DIRECTION_INDICATOR_NOT_VALID:
2022 dev_err(&req->adapter->ccw_device->dev,
b62a8d9b 2023 "Incorrect direction %d, LUN 0x%016Lx on port "
ff3b24fa 2024 "0x%016Lx closed\n",
c41f8cbd 2025 req->qtcb->bottom.io.data_direction,
b62a8d9b
CS
2026 (unsigned long long)zfcp_scsi_dev_lun(sdev),
2027 (unsigned long long)zfcp_sdev->port->wwpn);
2028 zfcp_erp_adapter_shutdown(zfcp_sdev->port->adapter, 0,
2029 "fssfch3", req);
c41f8cbd
SS
2030 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2031 break;
2032 case FSF_CMND_LENGTH_NOT_VALID:
2033 dev_err(&req->adapter->ccw_device->dev,
b62a8d9b 2034 "Incorrect CDB length %d, LUN 0x%016Lx on "
ff3b24fa 2035 "port 0x%016Lx closed\n",
c41f8cbd 2036 req->qtcb->bottom.io.fcp_cmnd_length,
b62a8d9b
CS
2037 (unsigned long long)zfcp_scsi_dev_lun(sdev),
2038 (unsigned long long)zfcp_sdev->port->wwpn);
2039 zfcp_erp_adapter_shutdown(zfcp_sdev->port->adapter, 0,
2040 "fssfch4", req);
c41f8cbd
SS
2041 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
2042 break;
2043 case FSF_PORT_BOXED:
edaed859
SS
2044 zfcp_erp_set_port_status(zfcp_sdev->port,
2045 ZFCP_STATUS_COMMON_ACCESS_BOXED);
2046 zfcp_erp_port_reopen(zfcp_sdev->port,
2047 ZFCP_STATUS_COMMON_ERP_FAILED, "fssfch5",
2048 req);
4c571c65 2049 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
2050 break;
2051 case FSF_LUN_BOXED:
edaed859
SS
2052 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_ACCESS_BOXED);
2053 zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
2054 "fssfch6", req);
4c571c65 2055 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
c41f8cbd
SS
2056 break;
2057 case FSF_ADAPTER_STATUS_AVAILABLE:
2058 if (header->fsf_status_qual.word[0] ==
2059 FSF_SQ_INVOKE_LINK_TEST_PROCEDURE)
b62a8d9b 2060 zfcp_fc_test_link(zfcp_sdev->port);
c41f8cbd 2061 req->status |= ZFCP_STATUS_FSFREQ_ERROR;
1da177e4 2062 break;
1da177e4 2063 }
c61b536c
CS
2064}
2065
2066static void zfcp_fsf_fcp_cmnd_handler(struct zfcp_fsf_req *req)
2067{
2068 struct scsi_cmnd *scpnt;
2069 struct fcp_resp_with_ext *fcp_rsp;
2070 unsigned long flags;
2071
2072 zfcp_fsf_fcp_handler_common(req);
2073
2074 read_lock_irqsave(&req->adapter->abort_lock, flags);
2075
2076 scpnt = req->data;
2077 if (unlikely(!scpnt)) {
2078 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
2079 return;
c41f8cbd 2080 }
c61b536c
CS
2081
2082 if (unlikely(req->status & ZFCP_STATUS_FSFREQ_ERROR)) {
2083 set_host_byte(scpnt, DID_TRANSPORT_DISRUPTED);
2084 goto skip_fsfstatus;
2085 }
2086
2087 switch (req->qtcb->header.fsf_status) {
2088 case FSF_INCONSISTENT_PROT_DATA:
2089 case FSF_INVALID_PROT_PARM:
2090 set_host_byte(scpnt, DID_ERROR);
2091 goto skip_fsfstatus;
2092 case FSF_BLOCK_GUARD_CHECK_FAILURE:
2093 zfcp_scsi_dif_sense_error(scpnt, 0x1);
2094 goto skip_fsfstatus;
2095 case FSF_APP_TAG_CHECK_FAILURE:
2096 zfcp_scsi_dif_sense_error(scpnt, 0x2);
2097 goto skip_fsfstatus;
2098 case FSF_REF_TAG_CHECK_FAILURE:
2099 zfcp_scsi_dif_sense_error(scpnt, 0x3);
2100 goto skip_fsfstatus;
2101 }
2102 fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
2103 zfcp_fc_eval_fcp_rsp(fcp_rsp, scpnt);
2104
2105skip_fsfstatus:
2106 zfcp_fsf_req_trace(req, scpnt);
2107 zfcp_dbf_scsi_result(req->adapter->dbf, scpnt, req);
2108
2109 scpnt->host_scribble = NULL;
2110 (scpnt->scsi_done) (scpnt);
2111 /*
2112 * We must hold this lock until scsi_done has been called.
2113 * Otherwise we may call scsi_done after abort regarding this
2114 * command has completed.
2115 * Note: scsi_done must not block!
2116 */
2117 read_unlock_irqrestore(&req->adapter->abort_lock, flags);
1da177e4
LT
2118}
2119
ef3eb71d
FB
2120static int zfcp_fsf_set_data_dir(struct scsi_cmnd *scsi_cmnd, u32 *data_dir)
2121{
2122 switch (scsi_get_prot_op(scsi_cmnd)) {
2123 case SCSI_PROT_NORMAL:
2124 switch (scsi_cmnd->sc_data_direction) {
2125 case DMA_NONE:
2126 *data_dir = FSF_DATADIR_CMND;
2127 break;
2128 case DMA_FROM_DEVICE:
2129 *data_dir = FSF_DATADIR_READ;
2130 break;
2131 case DMA_TO_DEVICE:
2132 *data_dir = FSF_DATADIR_WRITE;
2133 break;
2134 case DMA_BIDIRECTIONAL:
2135 return -EINVAL;
2136 }
2137 break;
2138
2139 case SCSI_PROT_READ_STRIP:
2140 *data_dir = FSF_DATADIR_DIF_READ_STRIP;
2141 break;
2142 case SCSI_PROT_WRITE_INSERT:
2143 *data_dir = FSF_DATADIR_DIF_WRITE_INSERT;
2144 break;
2145 case SCSI_PROT_READ_PASS:
2146 *data_dir = FSF_DATADIR_DIF_READ_CONVERT;
2147 break;
2148 case SCSI_PROT_WRITE_PASS:
2149 *data_dir = FSF_DATADIR_DIF_WRITE_CONVERT;
2150 break;
2151 default:
2152 return -EINVAL;
2153 }
2154
2155 return 0;
2156}
2157
c41f8cbd 2158/**
b62a8d9b 2159 * zfcp_fsf_fcp_cmnd - initiate an FCP command (for a SCSI command)
c41f8cbd 2160 * @scsi_cmnd: scsi command to be sent
1da177e4 2161 */
b62a8d9b 2162int zfcp_fsf_fcp_cmnd(struct scsi_cmnd *scsi_cmnd)
1da177e4 2163{
c41f8cbd 2164 struct zfcp_fsf_req *req;
4318e08c 2165 struct fcp_cmnd *fcp_cmnd;
bc90c863 2166 unsigned int sbtype = SBAL_FLAGS0_TYPE_READ;
ef3eb71d 2167 int real_bytes, retval = -EIO, dix_bytes = 0;
b62a8d9b
CS
2168 struct scsi_device *sdev = scsi_cmnd->device;
2169 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
2170 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
564e1c86 2171 struct zfcp_qdio *qdio = adapter->qdio;
ef3eb71d 2172 struct fsf_qtcb_bottom_io *io;
1da177e4 2173
b62a8d9b 2174 if (unlikely(!(atomic_read(&zfcp_sdev->status) &
c41f8cbd
SS
2175 ZFCP_STATUS_COMMON_UNBLOCKED)))
2176 return -EBUSY;
1da177e4 2177
564e1c86 2178 spin_lock(&qdio->req_q_lock);
706eca49 2179 if (atomic_read(&qdio->req_q_free) <= 0) {
564e1c86 2180 atomic_inc(&qdio->req_q_full);
c41f8cbd 2181 goto out;
8fdf30d5 2182 }
09a46c6e 2183
1674b405
CS
2184 if (scsi_cmnd->sc_data_direction == DMA_TO_DEVICE)
2185 sbtype = SBAL_FLAGS0_TYPE_WRITE;
2186
564e1c86 2187 req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
1674b405 2188 sbtype, adapter->pool.scsi_req);
09a46c6e 2189
025270f0 2190 if (IS_ERR(req)) {
c41f8cbd
SS
2191 retval = PTR_ERR(req);
2192 goto out;
2193 }
2194
ef3eb71d
FB
2195 scsi_cmnd->host_scribble = (unsigned char *) req->req_id;
2196
2197 io = &req->qtcb->bottom.io;
09a46c6e 2198 req->status |= ZFCP_STATUS_FSFREQ_CLEANUP;
c41f8cbd 2199 req->data = scsi_cmnd;
c61b536c 2200 req->handler = zfcp_fsf_fcp_cmnd_handler;
b62a8d9b
CS
2201 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2202 req->qtcb->header.port_handle = zfcp_sdev->port->handle;
ef3eb71d
FB
2203 io->service_class = FSF_CLASS_3;
2204 io->fcp_cmnd_length = FCP_CMND_LEN;
c41f8cbd 2205
ef3eb71d
FB
2206 if (scsi_get_prot_op(scsi_cmnd) != SCSI_PROT_NORMAL) {
2207 io->data_block_length = scsi_cmnd->device->sector_size;
2208 io->ref_tag_value = scsi_get_lba(scsi_cmnd) & 0xFFFFFFFF;
1da177e4
LT
2209 }
2210
ef3eb71d
FB
2211 zfcp_fsf_set_data_dir(scsi_cmnd, &io->data_direction);
2212
4318e08c
CS
2213 fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
2214 zfcp_fc_scsi_to_fcp(fcp_cmnd, scsi_cmnd);
1da177e4 2215
ef3eb71d
FB
2216 if (scsi_prot_sg_count(scsi_cmnd)) {
2217 zfcp_qdio_set_data_div(qdio, &req->qdio_req,
2218 scsi_prot_sg_count(scsi_cmnd));
2219 dix_bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
2220 scsi_prot_sglist(scsi_cmnd));
2221 io->prot_data_length = dix_bytes;
2222 }
2223
1674b405 2224 real_bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req,
01b04759 2225 scsi_sglist(scsi_cmnd));
ef3eb71d
FB
2226
2227 if (unlikely(real_bytes < 0) || unlikely(dix_bytes < 0))
c41f8cbd 2228 goto failed_scsi_cmnd;
1da177e4 2229
ef3eb71d
FB
2230 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
2231
c41f8cbd
SS
2232 retval = zfcp_fsf_req_send(req);
2233 if (unlikely(retval))
2234 goto failed_scsi_cmnd;
1da177e4 2235
c41f8cbd 2236 goto out;
1da177e4 2237
c41f8cbd 2238failed_scsi_cmnd:
c41f8cbd
SS
2239 zfcp_fsf_req_free(req);
2240 scsi_cmnd->host_scribble = NULL;
2241out:
564e1c86 2242 spin_unlock(&qdio->req_q_lock);
c41f8cbd 2243 return retval;
1da177e4
LT
2244}
2245
c61b536c
CS
2246static void zfcp_fsf_fcp_task_mgmt_handler(struct zfcp_fsf_req *req)
2247{
2248 struct fcp_resp_with_ext *fcp_rsp;
2249 struct fcp_resp_rsp_info *rsp_info;
2250
2251 zfcp_fsf_fcp_handler_common(req);
2252
2253 fcp_rsp = (struct fcp_resp_with_ext *) &req->qtcb->bottom.io.fcp_rsp;
2254 rsp_info = (struct fcp_resp_rsp_info *) &fcp_rsp[1];
2255
2256 if ((rsp_info->rsp_code != FCP_TMF_CMPL) ||
2257 (req->status & ZFCP_STATUS_FSFREQ_ERROR))
2258 req->status |= ZFCP_STATUS_FSFREQ_TMFUNCFAILED;
2259}
2260
1da177e4 2261/**
b62a8d9b
CS
2262 * zfcp_fsf_fcp_task_mgmt - send SCSI task management command
2263 * @scmnd: SCSI command to send the task management command for
c41f8cbd 2264 * @tm_flags: unsigned byte for task management flags
c41f8cbd 2265 * Returns: on success pointer to struct fsf_req, NULL otherwise
1da177e4 2266 */
b62a8d9b
CS
2267struct zfcp_fsf_req *zfcp_fsf_fcp_task_mgmt(struct scsi_cmnd *scmnd,
2268 u8 tm_flags)
1da177e4 2269{
c41f8cbd 2270 struct zfcp_fsf_req *req = NULL;
4318e08c 2271 struct fcp_cmnd *fcp_cmnd;
b62a8d9b
CS
2272 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scmnd->device);
2273 struct zfcp_qdio *qdio = zfcp_sdev->port->adapter->qdio;
1da177e4 2274
b62a8d9b 2275 if (unlikely(!(atomic_read(&zfcp_sdev->status) &
c41f8cbd
SS
2276 ZFCP_STATUS_COMMON_UNBLOCKED)))
2277 return NULL;
1da177e4 2278
44a24cb3 2279 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 2280 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 2281 goto out;
09a46c6e 2282
564e1c86 2283 req = zfcp_fsf_req_create(qdio, FSF_QTCB_FCP_CMND,
1674b405 2284 SBAL_FLAGS0_TYPE_WRITE,
564e1c86 2285 qdio->adapter->pool.scsi_req);
09a46c6e 2286
633528c3
SS
2287 if (IS_ERR(req)) {
2288 req = NULL;
c41f8cbd 2289 goto out;
633528c3 2290 }
1da177e4 2291
c41f8cbd 2292 req->status |= ZFCP_STATUS_FSFREQ_TASK_MANAGEMENT;
b62a8d9b 2293 req->data = scmnd;
c61b536c 2294 req->handler = zfcp_fsf_fcp_task_mgmt_handler;
b62a8d9b
CS
2295 req->qtcb->header.lun_handle = zfcp_sdev->lun_handle;
2296 req->qtcb->header.port_handle = zfcp_sdev->port->handle;
c41f8cbd
SS
2297 req->qtcb->bottom.io.data_direction = FSF_DATADIR_CMND;
2298 req->qtcb->bottom.io.service_class = FSF_CLASS_3;
4318e08c 2299 req->qtcb->bottom.io.fcp_cmnd_length = FCP_CMND_LEN;
c41f8cbd 2300
1674b405 2301 zfcp_qdio_set_sbale_last(qdio, &req->qdio_req);
1da177e4 2302
4318e08c 2303 fcp_cmnd = (struct fcp_cmnd *) &req->qtcb->bottom.io.fcp_cmnd;
b62a8d9b 2304 zfcp_fc_fcp_tm(fcp_cmnd, scmnd->device, tm_flags);
1da177e4 2305
c41f8cbd
SS
2306 zfcp_fsf_start_timer(req, ZFCP_SCSI_ER_TIMEOUT);
2307 if (!zfcp_fsf_req_send(req))
2308 goto out;
1da177e4 2309
c41f8cbd
SS
2310 zfcp_fsf_req_free(req);
2311 req = NULL;
2312out:
44a24cb3 2313 spin_unlock_irq(&qdio->req_q_lock);
c41f8cbd
SS
2314 return req;
2315}
1da177e4 2316
c41f8cbd
SS
2317static void zfcp_fsf_control_file_handler(struct zfcp_fsf_req *req)
2318{
1da177e4
LT
2319}
2320
c41f8cbd
SS
2321/**
2322 * zfcp_fsf_control_file - control file upload/download
2323 * @adapter: pointer to struct zfcp_adapter
2324 * @fsf_cfdc: pointer to struct zfcp_fsf_cfdc
2325 * Returns: on success pointer to struct zfcp_fsf_req, NULL otherwise
1da177e4 2326 */
c41f8cbd
SS
2327struct zfcp_fsf_req *zfcp_fsf_control_file(struct zfcp_adapter *adapter,
2328 struct zfcp_fsf_cfdc *fsf_cfdc)
1da177e4 2329{
564e1c86 2330 struct zfcp_qdio *qdio = adapter->qdio;
c41f8cbd
SS
2331 struct zfcp_fsf_req *req = NULL;
2332 struct fsf_qtcb_bottom_support *bottom;
2333 int direction, retval = -EIO, bytes;
2334
2335 if (!(adapter->adapter_features & FSF_FEATURE_CFDC))
2336 return ERR_PTR(-EOPNOTSUPP);
2337
2338 switch (fsf_cfdc->command) {
2339 case FSF_QTCB_DOWNLOAD_CONTROL_FILE:
2340 direction = SBAL_FLAGS0_TYPE_WRITE;
2341 break;
2342 case FSF_QTCB_UPLOAD_CONTROL_FILE:
2343 direction = SBAL_FLAGS0_TYPE_READ;
2344 break;
2345 default:
2346 return ERR_PTR(-EINVAL);
2347 }
1da177e4 2348
44a24cb3 2349 spin_lock_irq(&qdio->req_q_lock);
6b9e1520 2350 if (zfcp_qdio_sbal_get(qdio))
c41f8cbd 2351 goto out;
1da177e4 2352
1674b405 2353 req = zfcp_fsf_req_create(qdio, fsf_cfdc->command, direction, NULL);
025270f0 2354 if (IS_ERR(req)) {
c41f8cbd
SS
2355 retval = -EPERM;
2356 goto out;
2357 }
1da177e4 2358
c41f8cbd 2359 req->handler = zfcp_fsf_control_file_handler;
1da177e4 2360
c41f8cbd
SS
2361 bottom = &req->qtcb->bottom.support;
2362 bottom->operation_subtype = FSF_CFDC_OPERATION_SUBTYPE;
2363 bottom->option = fsf_cfdc->option;
8a36e453 2364
01b04759
SS
2365 bytes = zfcp_qdio_sbals_from_sg(qdio, &req->qdio_req, fsf_cfdc->sg);
2366
c41f8cbd 2367 if (bytes != ZFCP_CFDC_MAX_SIZE) {
c41f8cbd
SS
2368 zfcp_fsf_req_free(req);
2369 goto out;
2370 }
ef3eb71d 2371 zfcp_qdio_set_sbale_last(adapter->qdio, &req->qdio_req);
1da177e4 2372
c41f8cbd
SS
2373 zfcp_fsf_start_timer(req, ZFCP_FSF_REQUEST_TIMEOUT);
2374 retval = zfcp_fsf_req_send(req);
2375out:
44a24cb3 2376 spin_unlock_irq(&qdio->req_q_lock);
8a36e453 2377
c41f8cbd 2378 if (!retval) {
058b8647 2379 wait_for_completion(&req->completion);
c41f8cbd 2380 return req;
1da177e4 2381 }
c41f8cbd 2382 return ERR_PTR(retval);
1da177e4 2383}
bd63eaf4
SS
2384
2385/**
2386 * zfcp_fsf_reqid_check - validate req_id contained in SBAL returned by QDIO
2387 * @adapter: pointer to struct zfcp_adapter
2388 * @sbal_idx: response queue index of SBAL to be processed
2389 */
564e1c86 2390void zfcp_fsf_reqid_check(struct zfcp_qdio *qdio, int sbal_idx)
bd63eaf4 2391{
564e1c86 2392 struct zfcp_adapter *adapter = qdio->adapter;
706eca49 2393 struct qdio_buffer *sbal = qdio->res_q[sbal_idx];
bd63eaf4
SS
2394 struct qdio_buffer_element *sbale;
2395 struct zfcp_fsf_req *fsf_req;
b6bd2fb9 2396 unsigned long req_id;
bd63eaf4
SS
2397 int idx;
2398
2399 for (idx = 0; idx < QDIO_MAX_ELEMENTS_PER_BUFFER; idx++) {
2400
2401 sbale = &sbal->element[idx];
2402 req_id = (unsigned long) sbale->addr;
b6bd2fb9 2403 fsf_req = zfcp_reqlist_find_rm(adapter->req_list, req_id);
bd63eaf4 2404
339f4f4e 2405 if (!fsf_req) {
bd63eaf4
SS
2406 /*
2407 * Unknown request means that we have potentially memory
2408 * corruption and must stop the machine immediately.
2409 */
339f4f4e 2410 zfcp_qdio_siosl(adapter);
bd63eaf4
SS
2411 panic("error: unknown req_id (%lx) on adapter %s.\n",
2412 req_id, dev_name(&adapter->ccw_device->dev));
339f4f4e 2413 }
bd63eaf4 2414
34c2b712 2415 fsf_req->qdio_req.sbal_response = sbal_idx;
bd63eaf4
SS
2416 zfcp_fsf_req_complete(fsf_req);
2417
2418 if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY))
2419 break;
2420 }
2421}