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