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