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