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