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