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