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