Merge tag 'v3.10.108' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / s390 / scsi / zfcp_scsi.c
CommitLineData
41fa2ada 1/*
553448f6 2 * zfcp device driver
1da177e4 3 *
553448f6 4 * Interface to Linux SCSI midlayer.
41fa2ada 5 *
54539504 6 * Copyright IBM Corp. 2002, 2016
1da177e4
LT
7 */
8
ecf39d42
CS
9#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
3a4c5d59 12#include <linux/module.h>
4318e08c 13#include <linux/types.h>
5a0e3ad6 14#include <linux/slab.h>
4318e08c 15#include <scsi/fc/fc_fcp.h>
ef3eb71d 16#include <scsi/scsi_eh.h>
60063497 17#include <linux/atomic.h>
dcd20e23
CS
18#include "zfcp_ext.h"
19#include "zfcp_dbf.h"
7c7dc196 20#include "zfcp_fc.h"
b6bd2fb9 21#include "zfcp_reqlist.h"
1da177e4 22
a40a1baf
CS
23static unsigned int default_depth = 32;
24module_param_named(queue_depth, default_depth, uint, 0600);
25MODULE_PARM_DESC(queue_depth, "Default queue depth for new SCSI devices");
26
ef3eb71d 27static bool enable_dif;
cc405ace 28module_param_named(dif, enable_dif, bool, 0400);
ef3eb71d 29MODULE_PARM_DESC(dif, "Enable DIF/DIX data integrity support");
ef3eb71d 30
0d81b4e8
CS
31static bool allow_lun_scan = 1;
32module_param(allow_lun_scan, bool, 0600);
33MODULE_PARM_DESC(allow_lun_scan, "For NPIV, scan and attach all storage LUNs");
34
e881a172
MC
35static int zfcp_scsi_change_queue_depth(struct scsi_device *sdev, int depth,
36 int reason)
a40a1baf 37{
42e62a74
CS
38 switch (reason) {
39 case SCSI_QDEPTH_DEFAULT:
40 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
41 break;
42 case SCSI_QDEPTH_QFULL:
43 scsi_track_queue_full(sdev, depth);
44 break;
45 case SCSI_QDEPTH_RAMP_UP:
46 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
47 break;
48 default:
e881a172 49 return -EOPNOTSUPP;
42e62a74 50 }
a40a1baf
CS
51 return sdev->queue_depth;
52}
53
b62a8d9b 54static void zfcp_scsi_slave_destroy(struct scsi_device *sdev)
1da177e4 55{
b62a8d9b
CS
56 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
57
44f747ff
SM
58 /* if previous slave_alloc returned early, there is nothing to do */
59 if (!zfcp_sdev->port)
60 return;
61
b62a8d9b
CS
62 zfcp_erp_lun_shutdown_wait(sdev, "scssd_1");
63 put_device(&zfcp_sdev->port->dev);
1da177e4
LT
64}
65
f76af7d7 66static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
1da177e4
LT
67{
68 if (sdp->tagged_supported)
a40a1baf 69 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, default_depth);
1da177e4
LT
70 else
71 scsi_adjust_queue_depth(sdp, 0, 1);
72 return 0;
73}
74
f76af7d7 75static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
1da177e4 76{
feac6a07 77 set_host_byte(scpnt, result);
250a1352 78 zfcp_dbf_scsi_fail_send(scpnt);
1da177e4
LT
79 scpnt->scsi_done(scpnt);
80}
81
e55f8753
CS
82static
83int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt)
1da177e4 84{
b62a8d9b 85 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
a2fa0aed 86 struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
b62a8d9b 87 int status, scsi_result, ret;
1da177e4
LT
88
89 /* reset the status for this request */
90 scpnt->result = 0;
91 scpnt->host_scribble = NULL;
1da177e4 92
a2fa0aed
CS
93 scsi_result = fc_remote_port_chkready(rport);
94 if (unlikely(scsi_result)) {
95 scpnt->result = scsi_result;
250a1352 96 zfcp_dbf_scsi_fail_send(scpnt);
a2fa0aed
CS
97 scpnt->scsi_done(scpnt);
98 return 0;
99 }
100
b62a8d9b 101 status = atomic_read(&zfcp_sdev->status);
8830271c 102 if (unlikely(status & ZFCP_STATUS_COMMON_ERP_FAILED) &&
b62a8d9b 103 !(atomic_read(&zfcp_sdev->port->status) &
8830271c 104 ZFCP_STATUS_COMMON_ERP_FAILED)) {
b62a8d9b 105 /* only LUN access denied, but port is good
8830271c 106 * not covered by FC transport, have to fail here */
f76af7d7 107 zfcp_scsi_command_fail(scpnt, DID_ERROR);
a419aef8 108 return 0;
f76af7d7
MP
109 }
110
8830271c 111 if (unlikely(!(status & ZFCP_STATUS_COMMON_UNBLOCKED))) {
1c2287b9 112 /* This could be
8830271c
CS
113 * call to rport_delete pending: mimic retry from
114 * fc_remote_port_chkready until rport is BLOCKED
115 */
116 zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY);
117 return 0;
118 }
119
b62a8d9b 120 ret = zfcp_fsf_fcp_cmnd(scpnt);
f76af7d7 121 if (unlikely(ret == -EBUSY))
f7a65e92 122 return SCSI_MLQUEUE_DEVICE_BUSY;
f76af7d7
MP
123 else if (unlikely(ret < 0))
124 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4 125
f76af7d7 126 return ret;
1da177e4
LT
127}
128
b62a8d9b 129static int zfcp_scsi_slave_alloc(struct scsi_device *sdev)
1da177e4 130{
b62a8d9b
CS
131 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
132 struct zfcp_adapter *adapter =
133 (struct zfcp_adapter *) sdev->host->hostdata[0];
134 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
1da177e4 135 struct zfcp_port *port;
f76af7d7 136 struct zfcp_unit *unit;
0d81b4e8 137 int npiv = adapter->connection_features & FSF_FEATURE_NPIV_MODE;
1da177e4 138
b62a8d9b
CS
139 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
140 if (!port)
141 return -ENXIO;
f76af7d7 142
b62a8d9b
CS
143 unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
144 if (unit)
145 put_device(&unit->dev);
f76af7d7 146
0d81b4e8 147 if (!unit && !(allow_lun_scan && npiv)) {
b62a8d9b
CS
148 put_device(&port->dev);
149 return -ENXIO;
f76af7d7 150 }
f76af7d7 151
b62a8d9b
CS
152 zfcp_sdev->port = port;
153 zfcp_sdev->latencies.write.channel.min = 0xFFFFFFFF;
154 zfcp_sdev->latencies.write.fabric.min = 0xFFFFFFFF;
155 zfcp_sdev->latencies.read.channel.min = 0xFFFFFFFF;
156 zfcp_sdev->latencies.read.fabric.min = 0xFFFFFFFF;
157 zfcp_sdev->latencies.cmd.channel.min = 0xFFFFFFFF;
158 zfcp_sdev->latencies.cmd.fabric.min = 0xFFFFFFFF;
159 spin_lock_init(&zfcp_sdev->latencies.lock);
f76af7d7 160
edaed859 161 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
ea4a3a6a 162 zfcp_erp_lun_reopen(sdev, 0, "scsla_1");
b62a8d9b
CS
163 zfcp_erp_wait(port->adapter);
164
165 return 0;
1da177e4
LT
166}
167
2b67fc46 168static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
1da177e4 169{
63caf367
CS
170 struct Scsi_Host *scsi_host = scpnt->device->host;
171 struct zfcp_adapter *adapter =
172 (struct zfcp_adapter *) scsi_host->hostdata[0];
63caf367 173 struct zfcp_fsf_req *old_req, *abrt_req;
1da177e4 174 unsigned long flags;
a11a52be 175 unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
a1dbfddd 176 int retval = SUCCESS, ret;
63caf367 177 int retry = 3;
a11a52be 178 char *dbf_tag;
1da177e4 179
059c97d0 180 /* avoid race condition between late normal completion and abort */
1da177e4
LT
181 write_lock_irqsave(&adapter->abort_lock, flags);
182
b6bd2fb9 183 old_req = zfcp_reqlist_find(adapter->req_list, old_reqid);
63caf367 184 if (!old_req) {
1da177e4 185 write_unlock_irqrestore(&adapter->abort_lock, flags);
250a1352 186 zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL);
c6936e7f 187 return FAILED; /* completion could be in progress */
1da177e4 188 }
63caf367 189 old_req->data = NULL;
1da177e4 190
4eff4a36 191 /* don't access old fsf_req after releasing the abort_lock */
1da177e4 192 write_unlock_irqrestore(&adapter->abort_lock, flags);
4eff4a36 193
63caf367 194 while (retry--) {
b62a8d9b 195 abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt);
63caf367
CS
196 if (abrt_req)
197 break;
198
199 zfcp_erp_wait(adapter);
a1dbfddd 200 ret = fc_block_scsi_eh(scpnt);
250a1352
SS
201 if (ret) {
202 zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL);
a1dbfddd 203 return ret;
250a1352 204 }
63caf367
CS
205 if (!(atomic_read(&adapter->status) &
206 ZFCP_STATUS_COMMON_RUNNING)) {
250a1352 207 zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL);
63caf367
CS
208 return SUCCESS;
209 }
1da177e4 210 }
250a1352
SS
211 if (!abrt_req) {
212 zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL);
63caf367 213 return FAILED;
250a1352 214 }
1da177e4 215
058b8647 216 wait_for_completion(&abrt_req->completion);
059c97d0 217
63caf367 218 if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
250a1352 219 dbf_tag = "abrt_ok";
63caf367 220 else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
250a1352 221 dbf_tag = "abrt_nn";
63caf367 222 else {
250a1352 223 dbf_tag = "abrt_fa";
1da177e4 224 retval = FAILED;
1da177e4 225 }
250a1352 226 zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req);
63caf367 227 zfcp_fsf_req_free(abrt_req);
f76af7d7 228 return retval;
1da177e4
LT
229}
230
54539504
BB
231struct zfcp_scsi_req_filter {
232 u8 tmf_scope;
233 u32 lun_handle;
234 u32 port_handle;
235};
236
237static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
238{
239 struct zfcp_scsi_req_filter *filter =
240 (struct zfcp_scsi_req_filter *)data;
241
242 /* already aborted - prevent side-effects - or not a SCSI command */
243 if (old_req->data == NULL || old_req->fsf_command != FSF_QTCB_FCP_CMND)
244 return;
245
246 /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
247 if (old_req->qtcb->header.port_handle != filter->port_handle)
248 return;
249
250 if (filter->tmf_scope == FCP_TMF_LUN_RESET &&
251 old_req->qtcb->header.lun_handle != filter->lun_handle)
252 return;
253
254 zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
255 old_req->data = NULL;
256}
257
258static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags)
259{
260 struct zfcp_adapter *adapter = zsdev->port->adapter;
261 struct zfcp_scsi_req_filter filter = {
262 .tmf_scope = FCP_TMF_TGT_RESET,
263 .port_handle = zsdev->port->handle,
264 };
265 unsigned long flags;
266
267 if (tm_flags == FCP_TMF_LUN_RESET) {
268 filter.tmf_scope = FCP_TMF_LUN_RESET;
269 filter.lun_handle = zsdev->lun_handle;
270 }
271
272 /*
273 * abort_lock secures against other processings - in the abort-function
274 * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
275 */
276 write_lock_irqsave(&adapter->abort_lock, flags);
277 zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd,
278 &filter);
279 write_unlock_irqrestore(&adapter->abort_lock, flags);
280}
281
63caf367 282static int zfcp_task_mgmt_function(struct scsi_cmnd *scpnt, u8 tm_flags)
1da177e4 283{
b62a8d9b
CS
284 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
285 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
564e1c86 286 struct zfcp_fsf_req *fsf_req = NULL;
a1dbfddd 287 int retval = SUCCESS, ret;
63caf367
CS
288 int retry = 3;
289
290 while (retry--) {
b62a8d9b 291 fsf_req = zfcp_fsf_fcp_task_mgmt(scpnt, tm_flags);
63caf367
CS
292 if (fsf_req)
293 break;
294
295 zfcp_erp_wait(adapter);
a1dbfddd 296 ret = fc_block_scsi_eh(scpnt);
2c7bfb34
SM
297 if (ret) {
298 zfcp_dbf_scsi_devreset("fiof", scpnt, tm_flags);
a1dbfddd 299 return ret;
2c7bfb34 300 }
a1dbfddd 301
63caf367
CS
302 if (!(atomic_read(&adapter->status) &
303 ZFCP_STATUS_COMMON_RUNNING)) {
b62a8d9b 304 zfcp_dbf_scsi_devreset("nres", scpnt, tm_flags);
63caf367
CS
305 return SUCCESS;
306 }
1da177e4 307 }
2c7bfb34
SM
308 if (!fsf_req) {
309 zfcp_dbf_scsi_devreset("reqf", scpnt, tm_flags);
63caf367 310 return FAILED;
2c7bfb34 311 }
1da177e4 312
058b8647 313 wait_for_completion(&fsf_req->completion);
77eb1699 314
8a36e453 315 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
b62a8d9b 316 zfcp_dbf_scsi_devreset("fail", scpnt, tm_flags);
f76af7d7 317 retval = FAILED;
54539504 318 } else {
b62a8d9b 319 zfcp_dbf_scsi_devreset("okay", scpnt, tm_flags);
54539504
BB
320 zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags);
321 }
77eb1699
AH
322
323 zfcp_fsf_req_free(fsf_req);
1da177e4
LT
324 return retval;
325}
326
f76af7d7
MP
327static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
328{
4318e08c 329 return zfcp_task_mgmt_function(scpnt, FCP_TMF_LUN_RESET);
f76af7d7
MP
330}
331
332static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
333{
4318e08c 334 return zfcp_task_mgmt_function(scpnt, FCP_TMF_TGT_RESET);
f76af7d7
MP
335}
336
2b67fc46 337static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
1da177e4 338{
b62a8d9b
CS
339 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
340 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
a1dbfddd 341 int ret;
1da177e4 342
ea4a3a6a 343 zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
81654286 344 zfcp_erp_wait(adapter);
a1dbfddd
CS
345 ret = fc_block_scsi_eh(scpnt);
346 if (ret)
347 return ret;
1da177e4 348
810f1e3e 349 return SUCCESS;
1da177e4
LT
350}
351
1947c72a
CS
352struct scsi_transport_template *zfcp_scsi_transport_template;
353
354static struct scsi_host_template zfcp_scsi_host_template = {
355 .module = THIS_MODULE,
356 .name = "zfcp",
357 .queuecommand = zfcp_scsi_queuecommand,
358 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
359 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
360 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
361 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
362 .slave_alloc = zfcp_scsi_slave_alloc,
363 .slave_configure = zfcp_scsi_slave_configure,
364 .slave_destroy = zfcp_scsi_slave_destroy,
365 .change_queue_depth = zfcp_scsi_change_queue_depth,
366 .proc_name = "zfcp",
367 .can_queue = 4096,
368 .this_id = -1,
b22b8386
SM
369 .sg_tablesize = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
370 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
371 /* GCD, adjusted later */
372 .max_sectors = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
373 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
374 /* GCD, adjusted later */
1947c72a
CS
375 .dma_boundary = ZFCP_QDIO_SBALE_LEN - 1,
376 .cmd_per_lun = 1,
377 .use_clustering = 1,
378 .shost_attrs = zfcp_sysfs_shost_attrs,
379 .sdev_attrs = zfcp_sysfs_sdev_attrs,
380};
381
382/**
383 * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
384 * @adapter: The zfcp adapter to register with the SCSI midlayer
385 */
386int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
1da177e4 387{
f76af7d7 388 struct ccw_dev_id dev_id;
1da177e4 389
9f28745a 390 if (adapter->scsi_host)
f76af7d7 391 return 0;
9f28745a 392
f76af7d7 393 ccw_device_get_id(adapter->ccw_device, &dev_id);
1da177e4 394 /* register adapter as SCSI host with mid layer of SCSI stack */
1947c72a 395 adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
1da177e4
LT
396 sizeof (struct zfcp_adapter *));
397 if (!adapter->scsi_host) {
553448f6 398 dev_err(&adapter->ccw_device->dev,
ff3b24fa
CS
399 "Registering the FCP device with the "
400 "SCSI stack failed\n");
f76af7d7 401 return -EIO;
1da177e4 402 }
1da177e4
LT
403
404 /* tell the SCSI stack some characteristics of this adapter */
f8210e34
CS
405 adapter->scsi_host->max_id = 511;
406 adapter->scsi_host->max_lun = 0xFFFFFFFF;
1da177e4 407 adapter->scsi_host->max_channel = 0;
f76af7d7 408 adapter->scsi_host->unique_id = dev_id.devno;
4318e08c 409 adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
1947c72a 410 adapter->scsi_host->transportt = zfcp_scsi_transport_template;
1da177e4 411
1da177e4
LT
412 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
413
414 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
415 scsi_host_put(adapter->scsi_host);
f76af7d7 416 return -EIO;
1da177e4 417 }
f76af7d7
MP
418
419 return 0;
1da177e4
LT
420}
421
1947c72a
CS
422/**
423 * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
424 * @adapter: The zfcp adapter to unregister.
425 */
426void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
1da177e4
LT
427{
428 struct Scsi_Host *shost;
13e1e1f0 429 struct zfcp_port *port;
1da177e4
LT
430
431 shost = adapter->scsi_host;
432 if (!shost)
433 return;
f76af7d7 434
ecf0c772
SS
435 read_lock_irq(&adapter->port_list_lock);
436 list_for_each_entry(port, &adapter->port_list, list)
f3450c7b 437 port->rport = NULL;
ecf0c772 438 read_unlock_irq(&adapter->port_list_lock);
f76af7d7 439
3859f6a2 440 fc_remove_host(shost);
1da177e4
LT
441 scsi_remove_host(shost);
442 scsi_host_put(shost);
443 adapter->scsi_host = NULL;
1da177e4
LT
444}
445
f6cd94b1
AH
446static struct fc_host_statistics*
447zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
1da177e4 448{
f6cd94b1 449 struct fc_host_statistics *fc_stats;
1da177e4 450
f6cd94b1
AH
451 if (!adapter->fc_stats) {
452 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
453 if (!fc_stats)
454 return NULL;
f3450c7b 455 adapter->fc_stats = fc_stats; /* freed in adapter_release */
f6cd94b1
AH
456 }
457 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
458 return adapter->fc_stats;
1da177e4
LT
459}
460
f76af7d7
MP
461static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
462 struct fsf_qtcb_bottom_port *data,
463 struct fsf_qtcb_bottom_port *old)
1da177e4 464{
f76af7d7
MP
465 fc_stats->seconds_since_last_reset =
466 data->seconds_since_last_reset - old->seconds_since_last_reset;
f6cd94b1
AH
467 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
468 fc_stats->tx_words = data->tx_words - old->tx_words;
469 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
470 fc_stats->rx_words = data->rx_words - old->rx_words;
471 fc_stats->lip_count = data->lip - old->lip;
472 fc_stats->nos_count = data->nos - old->nos;
473 fc_stats->error_frames = data->error_frames - old->error_frames;
474 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
475 fc_stats->link_failure_count = data->link_failure - old->link_failure;
476 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
f76af7d7
MP
477 fc_stats->loss_of_signal_count =
478 data->loss_of_signal - old->loss_of_signal;
479 fc_stats->prim_seq_protocol_err_count =
480 data->psp_error_counts - old->psp_error_counts;
481 fc_stats->invalid_tx_word_count =
482 data->invalid_tx_words - old->invalid_tx_words;
f6cd94b1 483 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
f76af7d7
MP
484 fc_stats->fcp_input_requests =
485 data->input_requests - old->input_requests;
486 fc_stats->fcp_output_requests =
487 data->output_requests - old->output_requests;
488 fc_stats->fcp_control_requests =
489 data->control_requests - old->control_requests;
f6cd94b1
AH
490 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
491 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
492}
1da177e4 493
f76af7d7
MP
494static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
495 struct fsf_qtcb_bottom_port *data)
f6cd94b1
AH
496{
497 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
498 fc_stats->tx_frames = data->tx_frames;
499 fc_stats->tx_words = data->tx_words;
500 fc_stats->rx_frames = data->rx_frames;
501 fc_stats->rx_words = data->rx_words;
502 fc_stats->lip_count = data->lip;
503 fc_stats->nos_count = data->nos;
504 fc_stats->error_frames = data->error_frames;
505 fc_stats->dumped_frames = data->dumped_frames;
506 fc_stats->link_failure_count = data->link_failure;
507 fc_stats->loss_of_sync_count = data->loss_of_sync;
508 fc_stats->loss_of_signal_count = data->loss_of_signal;
509 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
510 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
511 fc_stats->invalid_crc_count = data->invalid_crcs;
512 fc_stats->fcp_input_requests = data->input_requests;
513 fc_stats->fcp_output_requests = data->output_requests;
514 fc_stats->fcp_control_requests = data->control_requests;
515 fc_stats->fcp_input_megabytes = data->input_mb;
516 fc_stats->fcp_output_megabytes = data->output_mb;
517}
518
f76af7d7 519static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
f6cd94b1
AH
520{
521 struct zfcp_adapter *adapter;
522 struct fc_host_statistics *fc_stats;
523 struct fsf_qtcb_bottom_port *data;
524 int ret;
525
f76af7d7 526 adapter = (struct zfcp_adapter *)host->hostdata[0];
f6cd94b1
AH
527 fc_stats = zfcp_init_fc_host_stats(adapter);
528 if (!fc_stats)
529 return NULL;
530
ec4081c6 531 data = kzalloc(sizeof(*data), GFP_KERNEL);
f6cd94b1
AH
532 if (!data)
533 return NULL;
f6cd94b1 534
564e1c86 535 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
f6cd94b1
AH
536 if (ret) {
537 kfree(data);
f76af7d7 538 return NULL;
f6cd94b1
AH
539 }
540
541 if (adapter->stats_reset &&
542 ((jiffies/HZ - adapter->stats_reset) <
f76af7d7 543 data->seconds_since_last_reset))
f6cd94b1
AH
544 zfcp_adjust_fc_host_stats(fc_stats, data,
545 adapter->stats_reset_data);
f76af7d7 546 else
f6cd94b1
AH
547 zfcp_set_fc_host_stats(fc_stats, data);
548
549 kfree(data);
550 return fc_stats;
1da177e4
LT
551}
552
f76af7d7 553static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
1da177e4 554{
f6cd94b1 555 struct zfcp_adapter *adapter;
f76af7d7 556 struct fsf_qtcb_bottom_port *data;
f6cd94b1 557 int ret;
1da177e4 558
f6cd94b1 559 adapter = (struct zfcp_adapter *)shost->hostdata[0];
ec4081c6 560 data = kzalloc(sizeof(*data), GFP_KERNEL);
f6cd94b1
AH
561 if (!data)
562 return;
f6cd94b1 563
564e1c86 564 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
f76af7d7 565 if (ret)
83f6d6d7 566 kfree(data);
f76af7d7 567 else {
f6cd94b1 568 adapter->stats_reset = jiffies/HZ;
f76af7d7 569 kfree(adapter->stats_reset_data);
f6cd94b1 570 adapter->stats_reset_data = data; /* finally freed in
f3450c7b 571 adapter_release */
f6cd94b1 572 }
1da177e4
LT
573}
574
85a82392
SS
575static void zfcp_get_host_port_state(struct Scsi_Host *shost)
576{
577 struct zfcp_adapter *adapter =
578 (struct zfcp_adapter *)shost->hostdata[0];
579 int status = atomic_read(&adapter->status);
580
581 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
582 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
583 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
584 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
585 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
586 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
587 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
588 else
589 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
590}
591
338151e0
AH
592static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
593{
594 rport->dev_loss_tmo = timeout;
595}
596
a2fa0aed
CS
597/**
598 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
599 * @rport: The FC rport where to teminate I/O
600 *
601 * Abort all pending SCSI commands for a port by closing the
835dc298
CS
602 * port. Using a reopen avoids a conflict with a shutdown
603 * overwriting a reopen. The "forced" ensures that a disappeared port
604 * is not opened again as valid due to the cached plogi data in
605 * non-NPIV mode.
a2fa0aed
CS
606 */
607static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
608{
70932935 609 struct zfcp_port *port;
ea945ff8
SS
610 struct Scsi_Host *shost = rport_to_shost(rport);
611 struct zfcp_adapter *adapter =
612 (struct zfcp_adapter *)shost->hostdata[0];
70932935 613
ea945ff8 614 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
a2fa0aed 615
70932935 616 if (port) {
ea4a3a6a 617 zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
615f59e0 618 put_device(&port->dev);
70932935 619 }
a2fa0aed
CS
620}
621
622static void zfcp_scsi_rport_register(struct zfcp_port *port)
623{
624 struct fc_rport_identifiers ids;
625 struct fc_rport *rport;
626
379d6bf6
CS
627 if (port->rport)
628 return;
629
a2fa0aed
CS
630 ids.node_name = port->wwnn;
631 ids.port_name = port->wwpn;
632 ids.port_id = port->d_id;
633 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
634
efaebcb4
SM
635 zfcp_dbf_rec_trig("scpaddy", port->adapter, port, NULL,
636 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
637 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
a2fa0aed
CS
638 rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
639 if (!rport) {
640 dev_err(&port->adapter->ccw_device->dev,
641 "Registering port 0x%016Lx failed\n",
642 (unsigned long long)port->wwpn);
643 return;
644 }
645
a2fa0aed
CS
646 rport->maxframe_size = port->maxframe_size;
647 rport->supported_classes = port->supported_classes;
648 port->rport = rport;
1bf3ff02 649 port->starget_id = rport->scsi_target_id;
5a7de559 650
1daa4eb5 651 zfcp_unit_queue_scsi_scan(port);
a2fa0aed
CS
652}
653
654static void zfcp_scsi_rport_block(struct zfcp_port *port)
655{
70932935
CS
656 struct fc_rport *rport = port->rport;
657
379d6bf6 658 if (rport) {
efaebcb4
SM
659 zfcp_dbf_rec_trig("scpdely", port->adapter, port, NULL,
660 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
661 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
70932935 662 fc_remote_port_delete(rport);
379d6bf6
CS
663 port->rport = NULL;
664 }
a2fa0aed
CS
665}
666
667void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
668{
615f59e0 669 get_device(&port->dev);
a2fa0aed
CS
670 port->rport_task = RPORT_ADD;
671
4544683a 672 if (!queue_work(port->adapter->work_queue, &port->rport_work))
615f59e0 673 put_device(&port->dev);
a2fa0aed
CS
674}
675
676void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
677{
615f59e0 678 get_device(&port->dev);
a2fa0aed
CS
679 port->rport_task = RPORT_DEL;
680
4544683a
SS
681 if (port->rport && queue_work(port->adapter->work_queue,
682 &port->rport_work))
a67417ab
SS
683 return;
684
615f59e0 685 put_device(&port->dev);
a2fa0aed
CS
686}
687
688void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
689{
ecf0c772 690 unsigned long flags;
a2fa0aed
CS
691 struct zfcp_port *port;
692
ecf0c772
SS
693 read_lock_irqsave(&adapter->port_list_lock, flags);
694 list_for_each_entry(port, &adapter->port_list, list)
a2fa0aed 695 zfcp_scsi_schedule_rport_block(port);
ecf0c772 696 read_unlock_irqrestore(&adapter->port_list_lock, flags);
a2fa0aed
CS
697}
698
699void zfcp_scsi_rport_work(struct work_struct *work)
700{
701 struct zfcp_port *port = container_of(work, struct zfcp_port,
702 rport_work);
703
704 while (port->rport_task) {
705 if (port->rport_task == RPORT_ADD) {
706 port->rport_task = RPORT_NONE;
707 zfcp_scsi_rport_register(port);
708 } else {
709 port->rport_task = RPORT_NONE;
710 zfcp_scsi_rport_block(port);
711 }
712 }
713
615f59e0 714 put_device(&port->dev);
a2fa0aed
CS
715}
716
ef3eb71d
FB
717/**
718 * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
719 * @adapter: The adapter where to configure DIF/DIX for the SCSI host
720 */
721void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
722{
723 unsigned int mask = 0;
724 unsigned int data_div;
725 struct Scsi_Host *shost = adapter->scsi_host;
726
727 data_div = atomic_read(&adapter->status) &
728 ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
729
730 if (enable_dif &&
731 adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
732 mask |= SHOST_DIF_TYPE1_PROTECTION;
733
734 if (enable_dif && data_div &&
735 adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
736 mask |= SHOST_DIX_TYPE1_PROTECTION;
737 scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
86a9668a
SS
738 shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
739 shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
740 shost->max_sectors = shost->sg_tablesize * 8;
ef3eb71d
FB
741 }
742
743 scsi_host_set_prot(shost, mask);
744}
745
746/**
747 * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
748 * @scmd: The SCSI command to report the error for
749 * @ascq: The ASCQ to put in the sense buffer
750 *
751 * See the error handling in sd_done for the sense codes used here.
752 * Set DID_SOFT_ERROR to retry the request, if possible.
753 */
754void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
755{
756 scsi_build_sense_buffer(1, scmd->sense_buffer,
757 ILLEGAL_REQUEST, 0x10, ascq);
758 set_driver_byte(scmd, DRIVER_SENSE);
759 scmd->result |= SAM_STAT_CHECK_CONDITION;
760 set_host_byte(scmd, DID_SOFT_ERROR);
761}
762
1da177e4 763struct fc_function_template zfcp_transport_functions = {
1da177e4
LT
764 .show_starget_port_id = 1,
765 .show_starget_port_name = 1,
766 .show_starget_node_name = 1,
3859f6a2 767 .show_rport_supported_classes = 1,
75bfc283 768 .show_rport_maxframe_size = 1,
338151e0 769 .show_rport_dev_loss_tmo = 1,
3859f6a2
AH
770 .show_host_node_name = 1,
771 .show_host_port_name = 1,
ad757cdf 772 .show_host_permanent_port_name = 1,
3859f6a2 773 .show_host_supported_classes = 1,
0fdd2133 774 .show_host_supported_fc4s = 1,
ad757cdf 775 .show_host_supported_speeds = 1,
13e1e1f0 776 .show_host_maxframe_size = 1,
3859f6a2 777 .show_host_serial_number = 1,
f6cd94b1
AH
778 .get_fc_host_stats = zfcp_get_fc_host_stats,
779 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
338151e0 780 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
85a82392 781 .get_host_port_state = zfcp_get_host_port_state,
a2fa0aed 782 .terminate_rport_io = zfcp_scsi_terminate_rport_io,
85a82392 783 .show_host_port_state = 1,
0fdd2133 784 .show_host_active_fc4s = 1,
7c7dc196 785 .bsg_request = zfcp_fc_exec_bsg_job,
491ca442 786 .bsg_timeout = zfcp_fc_timeout_bsg_job,
f6cd94b1
AH
787 /* no functions registered for following dynamic attributes but
788 directly set by LLDD */
ad757cdf 789 .show_host_port_type = 1,
038d9446 790 .show_host_symbolic_name = 1,
13e1e1f0
AH
791 .show_host_speed = 1,
792 .show_host_port_id = 1,
7c7dc196 793 .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
1da177e4 794};