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