xref: /openbmc/linux/drivers/s390/scsi/zfcp_scsi.c (revision 9fb29c73)
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 	zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
372 	zfcp_erp_wait(adapter);
373 	fc_ret = fc_block_scsi_eh(scpnt);
374 	if (fc_ret)
375 		ret = fc_ret;
376 
377 	zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
378 	return ret;
379 }
380 
381 /**
382  * zfcp_scsi_sysfs_host_reset() - Support scsi_host sysfs attribute host_reset.
383  * @shost: Pointer to Scsi_Host to perform action on.
384  * @reset_type: We support %SCSI_ADAPTER_RESET but not %SCSI_FIRMWARE_RESET.
385  *
386  * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise.
387  *
388  * This is similar to zfcp_sysfs_adapter_failed_store().
389  */
390 static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host *shost, int reset_type)
391 {
392 	struct zfcp_adapter *adapter =
393 		(struct zfcp_adapter *)shost->hostdata[0];
394 	int ret = 0;
395 
396 	if (reset_type != SCSI_ADAPTER_RESET) {
397 		ret = -EOPNOTSUPP;
398 		zfcp_dbf_scsi_eh("scshr_n", adapter, ~0, ret);
399 		return ret;
400 	}
401 
402 	zfcp_erp_adapter_reset_sync(adapter, "scshr_y");
403 	return ret;
404 }
405 
406 struct scsi_transport_template *zfcp_scsi_transport_template;
407 
408 static struct scsi_host_template zfcp_scsi_host_template = {
409 	.module			 = THIS_MODULE,
410 	.name			 = "zfcp",
411 	.queuecommand		 = zfcp_scsi_queuecommand,
412 	.eh_timed_out		 = fc_eh_timed_out,
413 	.eh_abort_handler	 = zfcp_scsi_eh_abort_handler,
414 	.eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
415 	.eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
416 	.eh_host_reset_handler	 = zfcp_scsi_eh_host_reset_handler,
417 	.slave_alloc		 = zfcp_scsi_slave_alloc,
418 	.slave_configure	 = zfcp_scsi_slave_configure,
419 	.slave_destroy		 = zfcp_scsi_slave_destroy,
420 	.change_queue_depth	 = scsi_change_queue_depth,
421 	.host_reset		 = zfcp_scsi_sysfs_host_reset,
422 	.proc_name		 = "zfcp",
423 	.can_queue		 = 4096,
424 	.this_id		 = -1,
425 	.sg_tablesize		 = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
426 				     * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
427 				   /* GCD, adjusted later */
428 	.max_sectors		 = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
429 				     * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
430 				   /* GCD, adjusted later */
431 	.dma_boundary		 = ZFCP_QDIO_SBALE_LEN - 1,
432 	.shost_attrs		 = zfcp_sysfs_shost_attrs,
433 	.sdev_attrs		 = zfcp_sysfs_sdev_attrs,
434 	.track_queue_depth	 = 1,
435 	.supported_mode		 = MODE_INITIATOR,
436 };
437 
438 /**
439  * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
440  * @adapter: The zfcp adapter to register with the SCSI midlayer
441  */
442 int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
443 {
444 	struct ccw_dev_id dev_id;
445 
446 	if (adapter->scsi_host)
447 		return 0;
448 
449 	ccw_device_get_id(adapter->ccw_device, &dev_id);
450 	/* register adapter as SCSI host with mid layer of SCSI stack */
451 	adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
452 					     sizeof (struct zfcp_adapter *));
453 	if (!adapter->scsi_host) {
454 		dev_err(&adapter->ccw_device->dev,
455 			"Registering the FCP device with the "
456 			"SCSI stack failed\n");
457 		return -EIO;
458 	}
459 
460 	/* tell the SCSI stack some characteristics of this adapter */
461 	adapter->scsi_host->max_id = 511;
462 	adapter->scsi_host->max_lun = 0xFFFFFFFF;
463 	adapter->scsi_host->max_channel = 0;
464 	adapter->scsi_host->unique_id = dev_id.devno;
465 	adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
466 	adapter->scsi_host->transportt = zfcp_scsi_transport_template;
467 
468 	adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
469 
470 	if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
471 		scsi_host_put(adapter->scsi_host);
472 		return -EIO;
473 	}
474 
475 	return 0;
476 }
477 
478 /**
479  * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
480  * @adapter: The zfcp adapter to unregister.
481  */
482 void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
483 {
484 	struct Scsi_Host *shost;
485 	struct zfcp_port *port;
486 
487 	shost = adapter->scsi_host;
488 	if (!shost)
489 		return;
490 
491 	read_lock_irq(&adapter->port_list_lock);
492 	list_for_each_entry(port, &adapter->port_list, list)
493 		port->rport = NULL;
494 	read_unlock_irq(&adapter->port_list_lock);
495 
496 	fc_remove_host(shost);
497 	scsi_remove_host(shost);
498 	scsi_host_put(shost);
499 	adapter->scsi_host = NULL;
500 }
501 
502 static struct fc_host_statistics*
503 zfcp_scsi_init_fc_host_stats(struct zfcp_adapter *adapter)
504 {
505 	struct fc_host_statistics *fc_stats;
506 
507 	if (!adapter->fc_stats) {
508 		fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
509 		if (!fc_stats)
510 			return NULL;
511 		adapter->fc_stats = fc_stats; /* freed in adapter_release */
512 	}
513 	memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
514 	return adapter->fc_stats;
515 }
516 
517 static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
518 					   struct fsf_qtcb_bottom_port *data,
519 					   struct fsf_qtcb_bottom_port *old)
520 {
521 	fc_stats->seconds_since_last_reset =
522 		data->seconds_since_last_reset - old->seconds_since_last_reset;
523 	fc_stats->tx_frames = data->tx_frames - old->tx_frames;
524 	fc_stats->tx_words = data->tx_words - old->tx_words;
525 	fc_stats->rx_frames = data->rx_frames - old->rx_frames;
526 	fc_stats->rx_words = data->rx_words - old->rx_words;
527 	fc_stats->lip_count = data->lip - old->lip;
528 	fc_stats->nos_count = data->nos - old->nos;
529 	fc_stats->error_frames = data->error_frames - old->error_frames;
530 	fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
531 	fc_stats->link_failure_count = data->link_failure - old->link_failure;
532 	fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
533 	fc_stats->loss_of_signal_count =
534 		data->loss_of_signal - old->loss_of_signal;
535 	fc_stats->prim_seq_protocol_err_count =
536 		data->psp_error_counts - old->psp_error_counts;
537 	fc_stats->invalid_tx_word_count =
538 		data->invalid_tx_words - old->invalid_tx_words;
539 	fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
540 	fc_stats->fcp_input_requests =
541 		data->input_requests - old->input_requests;
542 	fc_stats->fcp_output_requests =
543 		data->output_requests - old->output_requests;
544 	fc_stats->fcp_control_requests =
545 		data->control_requests - old->control_requests;
546 	fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
547 	fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
548 }
549 
550 static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics *fc_stats,
551 					struct fsf_qtcb_bottom_port *data)
552 {
553 	fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
554 	fc_stats->tx_frames = data->tx_frames;
555 	fc_stats->tx_words = data->tx_words;
556 	fc_stats->rx_frames = data->rx_frames;
557 	fc_stats->rx_words = data->rx_words;
558 	fc_stats->lip_count = data->lip;
559 	fc_stats->nos_count = data->nos;
560 	fc_stats->error_frames = data->error_frames;
561 	fc_stats->dumped_frames = data->dumped_frames;
562 	fc_stats->link_failure_count = data->link_failure;
563 	fc_stats->loss_of_sync_count = data->loss_of_sync;
564 	fc_stats->loss_of_signal_count = data->loss_of_signal;
565 	fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
566 	fc_stats->invalid_tx_word_count = data->invalid_tx_words;
567 	fc_stats->invalid_crc_count = data->invalid_crcs;
568 	fc_stats->fcp_input_requests = data->input_requests;
569 	fc_stats->fcp_output_requests = data->output_requests;
570 	fc_stats->fcp_control_requests = data->control_requests;
571 	fc_stats->fcp_input_megabytes = data->input_mb;
572 	fc_stats->fcp_output_megabytes = data->output_mb;
573 }
574 
575 static struct fc_host_statistics *
576 zfcp_scsi_get_fc_host_stats(struct Scsi_Host *host)
577 {
578 	struct zfcp_adapter *adapter;
579 	struct fc_host_statistics *fc_stats;
580 	struct fsf_qtcb_bottom_port *data;
581 	int ret;
582 
583 	adapter = (struct zfcp_adapter *)host->hostdata[0];
584 	fc_stats = zfcp_scsi_init_fc_host_stats(adapter);
585 	if (!fc_stats)
586 		return NULL;
587 
588 	data = kzalloc(sizeof(*data), GFP_KERNEL);
589 	if (!data)
590 		return NULL;
591 
592 	ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
593 	if (ret) {
594 		kfree(data);
595 		return NULL;
596 	}
597 
598 	if (adapter->stats_reset &&
599 	    ((jiffies/HZ - adapter->stats_reset) <
600 	     data->seconds_since_last_reset))
601 		zfcp_scsi_adjust_fc_host_stats(fc_stats, data,
602 					       adapter->stats_reset_data);
603 	else
604 		zfcp_scsi_set_fc_host_stats(fc_stats, data);
605 
606 	kfree(data);
607 	return fc_stats;
608 }
609 
610 static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host *shost)
611 {
612 	struct zfcp_adapter *adapter;
613 	struct fsf_qtcb_bottom_port *data;
614 	int ret;
615 
616 	adapter = (struct zfcp_adapter *)shost->hostdata[0];
617 	data = kzalloc(sizeof(*data), GFP_KERNEL);
618 	if (!data)
619 		return;
620 
621 	ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
622 	if (ret)
623 		kfree(data);
624 	else {
625 		adapter->stats_reset = jiffies/HZ;
626 		kfree(adapter->stats_reset_data);
627 		adapter->stats_reset_data = data; /* finally freed in
628 						     adapter_release */
629 	}
630 }
631 
632 static void zfcp_scsi_get_host_port_state(struct Scsi_Host *shost)
633 {
634 	struct zfcp_adapter *adapter =
635 		(struct zfcp_adapter *)shost->hostdata[0];
636 	int status = atomic_read(&adapter->status);
637 
638 	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
639 	    !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
640 		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
641 	else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
642 		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
643 	else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
644 		fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
645 	else
646 		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
647 }
648 
649 static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport *rport,
650 					     u32 timeout)
651 {
652 	rport->dev_loss_tmo = timeout;
653 }
654 
655 /**
656  * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
657  * @rport: The FC rport where to teminate I/O
658  *
659  * Abort all pending SCSI commands for a port by closing the
660  * port. Using a reopen avoids a conflict with a shutdown
661  * overwriting a reopen. The "forced" ensures that a disappeared port
662  * is not opened again as valid due to the cached plogi data in
663  * non-NPIV mode.
664  */
665 static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
666 {
667 	struct zfcp_port *port;
668 	struct Scsi_Host *shost = rport_to_shost(rport);
669 	struct zfcp_adapter *adapter =
670 		(struct zfcp_adapter *)shost->hostdata[0];
671 
672 	port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
673 
674 	if (port) {
675 		zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
676 		put_device(&port->dev);
677 	} else {
678 		zfcp_erp_port_forced_no_port_dbf(
679 			"sctrpin", adapter,
680 			rport->port_name /* zfcp_scsi_rport_register */,
681 			rport->port_id /* zfcp_scsi_rport_register */);
682 	}
683 }
684 
685 static void zfcp_scsi_rport_register(struct zfcp_port *port)
686 {
687 	struct fc_rport_identifiers ids;
688 	struct fc_rport *rport;
689 
690 	if (port->rport)
691 		return;
692 
693 	ids.node_name = port->wwnn;
694 	ids.port_name = port->wwpn;
695 	ids.port_id = port->d_id;
696 	ids.roles = FC_RPORT_ROLE_FCP_TARGET;
697 
698 	zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
699 			       ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
700 			       ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
701 	rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
702 	if (!rport) {
703 		dev_err(&port->adapter->ccw_device->dev,
704 			"Registering port 0x%016Lx failed\n",
705 			(unsigned long long)port->wwpn);
706 		return;
707 	}
708 
709 	rport->maxframe_size = port->maxframe_size;
710 	rport->supported_classes = port->supported_classes;
711 	port->rport = rport;
712 	port->starget_id = rport->scsi_target_id;
713 
714 	zfcp_unit_queue_scsi_scan(port);
715 }
716 
717 static void zfcp_scsi_rport_block(struct zfcp_port *port)
718 {
719 	struct fc_rport *rport = port->rport;
720 
721 	if (rport) {
722 		zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
723 				       ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
724 				       ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
725 		fc_remote_port_delete(rport);
726 		port->rport = NULL;
727 	}
728 }
729 
730 void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
731 {
732 	get_device(&port->dev);
733 	port->rport_task = RPORT_ADD;
734 
735 	if (!queue_work(port->adapter->work_queue, &port->rport_work))
736 		put_device(&port->dev);
737 }
738 
739 void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
740 {
741 	get_device(&port->dev);
742 	port->rport_task = RPORT_DEL;
743 
744 	if (port->rport && queue_work(port->adapter->work_queue,
745 				      &port->rport_work))
746 		return;
747 
748 	put_device(&port->dev);
749 }
750 
751 void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
752 {
753 	unsigned long flags;
754 	struct zfcp_port *port;
755 
756 	read_lock_irqsave(&adapter->port_list_lock, flags);
757 	list_for_each_entry(port, &adapter->port_list, list)
758 		zfcp_scsi_schedule_rport_block(port);
759 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
760 }
761 
762 void zfcp_scsi_rport_work(struct work_struct *work)
763 {
764 	struct zfcp_port *port = container_of(work, struct zfcp_port,
765 					      rport_work);
766 
767 	set_worker_desc("zrp%c-%16llx",
768 			(port->rport_task == RPORT_ADD) ? 'a' : 'd',
769 			port->wwpn); /* < WORKER_DESC_LEN=24 */
770 	while (port->rport_task) {
771 		if (port->rport_task == RPORT_ADD) {
772 			port->rport_task = RPORT_NONE;
773 			zfcp_scsi_rport_register(port);
774 		} else {
775 			port->rport_task = RPORT_NONE;
776 			zfcp_scsi_rport_block(port);
777 		}
778 	}
779 
780 	put_device(&port->dev);
781 }
782 
783 /**
784  * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
785  * @adapter: The adapter where to configure DIF/DIX for the SCSI host
786  */
787 void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
788 {
789 	unsigned int mask = 0;
790 	unsigned int data_div;
791 	struct Scsi_Host *shost = adapter->scsi_host;
792 
793 	data_div = atomic_read(&adapter->status) &
794 		   ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
795 
796 	if ((enable_dif || zfcp_experimental_dix) &&
797 	    adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
798 		mask |= SHOST_DIF_TYPE1_PROTECTION;
799 
800 	if (zfcp_experimental_dix && data_div &&
801 	    adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
802 		mask |= SHOST_DIX_TYPE1_PROTECTION;
803 		scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
804 		shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
805 		shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
806 		shost->max_sectors = shost->sg_tablesize * 8;
807 	}
808 
809 	scsi_host_set_prot(shost, mask);
810 }
811 
812 /**
813  * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
814  * @scmd: The SCSI command to report the error for
815  * @ascq: The ASCQ to put in the sense buffer
816  *
817  * See the error handling in sd_done for the sense codes used here.
818  * Set DID_SOFT_ERROR to retry the request, if possible.
819  */
820 void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
821 {
822 	scsi_build_sense_buffer(1, scmd->sense_buffer,
823 				ILLEGAL_REQUEST, 0x10, ascq);
824 	set_driver_byte(scmd, DRIVER_SENSE);
825 	scmd->result |= SAM_STAT_CHECK_CONDITION;
826 	set_host_byte(scmd, DID_SOFT_ERROR);
827 }
828 
829 struct fc_function_template zfcp_transport_functions = {
830 	.show_starget_port_id = 1,
831 	.show_starget_port_name = 1,
832 	.show_starget_node_name = 1,
833 	.show_rport_supported_classes = 1,
834 	.show_rport_maxframe_size = 1,
835 	.show_rport_dev_loss_tmo = 1,
836 	.show_host_node_name = 1,
837 	.show_host_port_name = 1,
838 	.show_host_permanent_port_name = 1,
839 	.show_host_supported_classes = 1,
840 	.show_host_supported_fc4s = 1,
841 	.show_host_supported_speeds = 1,
842 	.show_host_maxframe_size = 1,
843 	.show_host_serial_number = 1,
844 	.get_fc_host_stats = zfcp_scsi_get_fc_host_stats,
845 	.reset_fc_host_stats = zfcp_scsi_reset_fc_host_stats,
846 	.set_rport_dev_loss_tmo = zfcp_scsi_set_rport_dev_loss_tmo,
847 	.get_host_port_state = zfcp_scsi_get_host_port_state,
848 	.terminate_rport_io = zfcp_scsi_terminate_rport_io,
849 	.show_host_port_state = 1,
850 	.show_host_active_fc4s = 1,
851 	.bsg_request = zfcp_fc_exec_bsg_job,
852 	.bsg_timeout = zfcp_fc_timeout_bsg_job,
853 	/* no functions registered for following dynamic attributes but
854 	   directly set by LLDD */
855 	.show_host_port_type = 1,
856 	.show_host_symbolic_name = 1,
857 	.show_host_speed = 1,
858 	.show_host_port_id = 1,
859 	.dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
860 };
861