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