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 
zfcp_scsi_change_queue_depth(struct scsi_device * sdev,int depth,int reason)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 
zfcp_scsi_slave_destroy(struct scsi_device * sdev)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 
zfcp_scsi_slave_configure(struct scsi_device * sdp)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 
zfcp_scsi_command_fail(struct scsi_cmnd * scpnt,int result)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
zfcp_scsi_queuecommand(struct Scsi_Host * shost,struct scsi_cmnd * scpnt)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 
zfcp_scsi_slave_alloc(struct scsi_device * sdev)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 
zfcp_scsi_eh_abort_handler(struct scsi_cmnd * scpnt)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 
zfcp_task_mgmt_function(struct scsi_cmnd * scpnt,u8 tm_flags)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 
zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd * scpnt)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 
zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd * scpnt)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 
zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd * scpnt)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		 = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
315 				     * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
316 				   /* GCD, adjusted later */
317 	.max_sectors		 = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
318 				     * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
319 				   /* GCD, adjusted later */
320 	.dma_boundary		 = ZFCP_QDIO_SBALE_LEN - 1,
321 	.cmd_per_lun		 = 1,
322 	.use_clustering		 = 1,
323 	.shost_attrs		 = zfcp_sysfs_shost_attrs,
324 	.sdev_attrs		 = zfcp_sysfs_sdev_attrs,
325 };
326 
327 /**
328  * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
329  * @adapter: The zfcp adapter to register with the SCSI midlayer
330  */
zfcp_scsi_adapter_register(struct zfcp_adapter * adapter)331 int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
332 {
333 	struct ccw_dev_id dev_id;
334 
335 	if (adapter->scsi_host)
336 		return 0;
337 
338 	ccw_device_get_id(adapter->ccw_device, &dev_id);
339 	/* register adapter as SCSI host with mid layer of SCSI stack */
340 	adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
341 					     sizeof (struct zfcp_adapter *));
342 	if (!adapter->scsi_host) {
343 		dev_err(&adapter->ccw_device->dev,
344 			"Registering the FCP device with the "
345 			"SCSI stack failed\n");
346 		return -EIO;
347 	}
348 
349 	/* tell the SCSI stack some characteristics of this adapter */
350 	adapter->scsi_host->max_id = 511;
351 	adapter->scsi_host->max_lun = 0xFFFFFFFF;
352 	adapter->scsi_host->max_channel = 0;
353 	adapter->scsi_host->unique_id = dev_id.devno;
354 	adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
355 	adapter->scsi_host->transportt = zfcp_scsi_transport_template;
356 
357 	adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
358 
359 	if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
360 		scsi_host_put(adapter->scsi_host);
361 		return -EIO;
362 	}
363 
364 	return 0;
365 }
366 
367 /**
368  * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
369  * @adapter: The zfcp adapter to unregister.
370  */
zfcp_scsi_adapter_unregister(struct zfcp_adapter * adapter)371 void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
372 {
373 	struct Scsi_Host *shost;
374 	struct zfcp_port *port;
375 
376 	shost = adapter->scsi_host;
377 	if (!shost)
378 		return;
379 
380 	read_lock_irq(&adapter->port_list_lock);
381 	list_for_each_entry(port, &adapter->port_list, list)
382 		port->rport = NULL;
383 	read_unlock_irq(&adapter->port_list_lock);
384 
385 	fc_remove_host(shost);
386 	scsi_remove_host(shost);
387 	scsi_host_put(shost);
388 	adapter->scsi_host = NULL;
389 }
390 
391 static struct fc_host_statistics*
zfcp_init_fc_host_stats(struct zfcp_adapter * adapter)392 zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
393 {
394 	struct fc_host_statistics *fc_stats;
395 
396 	if (!adapter->fc_stats) {
397 		fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
398 		if (!fc_stats)
399 			return NULL;
400 		adapter->fc_stats = fc_stats; /* freed in adapter_release */
401 	}
402 	memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
403 	return adapter->fc_stats;
404 }
405 
zfcp_adjust_fc_host_stats(struct fc_host_statistics * fc_stats,struct fsf_qtcb_bottom_port * data,struct fsf_qtcb_bottom_port * old)406 static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
407 				      struct fsf_qtcb_bottom_port *data,
408 				      struct fsf_qtcb_bottom_port *old)
409 {
410 	fc_stats->seconds_since_last_reset =
411 		data->seconds_since_last_reset - old->seconds_since_last_reset;
412 	fc_stats->tx_frames = data->tx_frames - old->tx_frames;
413 	fc_stats->tx_words = data->tx_words - old->tx_words;
414 	fc_stats->rx_frames = data->rx_frames - old->rx_frames;
415 	fc_stats->rx_words = data->rx_words - old->rx_words;
416 	fc_stats->lip_count = data->lip - old->lip;
417 	fc_stats->nos_count = data->nos - old->nos;
418 	fc_stats->error_frames = data->error_frames - old->error_frames;
419 	fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
420 	fc_stats->link_failure_count = data->link_failure - old->link_failure;
421 	fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
422 	fc_stats->loss_of_signal_count =
423 		data->loss_of_signal - old->loss_of_signal;
424 	fc_stats->prim_seq_protocol_err_count =
425 		data->psp_error_counts - old->psp_error_counts;
426 	fc_stats->invalid_tx_word_count =
427 		data->invalid_tx_words - old->invalid_tx_words;
428 	fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
429 	fc_stats->fcp_input_requests =
430 		data->input_requests - old->input_requests;
431 	fc_stats->fcp_output_requests =
432 		data->output_requests - old->output_requests;
433 	fc_stats->fcp_control_requests =
434 		data->control_requests - old->control_requests;
435 	fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
436 	fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
437 }
438 
zfcp_set_fc_host_stats(struct fc_host_statistics * fc_stats,struct fsf_qtcb_bottom_port * data)439 static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
440 				   struct fsf_qtcb_bottom_port *data)
441 {
442 	fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
443 	fc_stats->tx_frames = data->tx_frames;
444 	fc_stats->tx_words = data->tx_words;
445 	fc_stats->rx_frames = data->rx_frames;
446 	fc_stats->rx_words = data->rx_words;
447 	fc_stats->lip_count = data->lip;
448 	fc_stats->nos_count = data->nos;
449 	fc_stats->error_frames = data->error_frames;
450 	fc_stats->dumped_frames = data->dumped_frames;
451 	fc_stats->link_failure_count = data->link_failure;
452 	fc_stats->loss_of_sync_count = data->loss_of_sync;
453 	fc_stats->loss_of_signal_count = data->loss_of_signal;
454 	fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
455 	fc_stats->invalid_tx_word_count = data->invalid_tx_words;
456 	fc_stats->invalid_crc_count = data->invalid_crcs;
457 	fc_stats->fcp_input_requests = data->input_requests;
458 	fc_stats->fcp_output_requests = data->output_requests;
459 	fc_stats->fcp_control_requests = data->control_requests;
460 	fc_stats->fcp_input_megabytes = data->input_mb;
461 	fc_stats->fcp_output_megabytes = data->output_mb;
462 }
463 
zfcp_get_fc_host_stats(struct Scsi_Host * host)464 static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
465 {
466 	struct zfcp_adapter *adapter;
467 	struct fc_host_statistics *fc_stats;
468 	struct fsf_qtcb_bottom_port *data;
469 	int ret;
470 
471 	adapter = (struct zfcp_adapter *)host->hostdata[0];
472 	fc_stats = zfcp_init_fc_host_stats(adapter);
473 	if (!fc_stats)
474 		return NULL;
475 
476 	data = kzalloc(sizeof(*data), GFP_KERNEL);
477 	if (!data)
478 		return NULL;
479 
480 	ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
481 	if (ret) {
482 		kfree(data);
483 		return NULL;
484 	}
485 
486 	if (adapter->stats_reset &&
487 	    ((jiffies/HZ - adapter->stats_reset) <
488 	     data->seconds_since_last_reset))
489 		zfcp_adjust_fc_host_stats(fc_stats, data,
490 					  adapter->stats_reset_data);
491 	else
492 		zfcp_set_fc_host_stats(fc_stats, data);
493 
494 	kfree(data);
495 	return fc_stats;
496 }
497 
zfcp_reset_fc_host_stats(struct Scsi_Host * shost)498 static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
499 {
500 	struct zfcp_adapter *adapter;
501 	struct fsf_qtcb_bottom_port *data;
502 	int ret;
503 
504 	adapter = (struct zfcp_adapter *)shost->hostdata[0];
505 	data = kzalloc(sizeof(*data), GFP_KERNEL);
506 	if (!data)
507 		return;
508 
509 	ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
510 	if (ret)
511 		kfree(data);
512 	else {
513 		adapter->stats_reset = jiffies/HZ;
514 		kfree(adapter->stats_reset_data);
515 		adapter->stats_reset_data = data; /* finally freed in
516 						     adapter_release */
517 	}
518 }
519 
zfcp_get_host_port_state(struct Scsi_Host * shost)520 static void zfcp_get_host_port_state(struct Scsi_Host *shost)
521 {
522 	struct zfcp_adapter *adapter =
523 		(struct zfcp_adapter *)shost->hostdata[0];
524 	int status = atomic_read(&adapter->status);
525 
526 	if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
527 	    !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
528 		fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
529 	else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
530 		fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
531 	else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
532 		fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
533 	else
534 		fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
535 }
536 
zfcp_set_rport_dev_loss_tmo(struct fc_rport * rport,u32 timeout)537 static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
538 {
539 	rport->dev_loss_tmo = timeout;
540 }
541 
542 /**
543  * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
544  * @rport: The FC rport where to teminate I/O
545  *
546  * Abort all pending SCSI commands for a port by closing the
547  * port. Using a reopen avoids a conflict with a shutdown
548  * overwriting a reopen. The "forced" ensures that a disappeared port
549  * is not opened again as valid due to the cached plogi data in
550  * non-NPIV mode.
551  */
zfcp_scsi_terminate_rport_io(struct fc_rport * rport)552 static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
553 {
554 	struct zfcp_port *port;
555 	struct Scsi_Host *shost = rport_to_shost(rport);
556 	struct zfcp_adapter *adapter =
557 		(struct zfcp_adapter *)shost->hostdata[0];
558 
559 	port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
560 
561 	if (port) {
562 		zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
563 		put_device(&port->dev);
564 	}
565 }
566 
zfcp_scsi_rport_register(struct zfcp_port * port)567 static void zfcp_scsi_rport_register(struct zfcp_port *port)
568 {
569 	struct fc_rport_identifiers ids;
570 	struct fc_rport *rport;
571 
572 	if (port->rport)
573 		return;
574 
575 	ids.node_name = port->wwnn;
576 	ids.port_name = port->wwpn;
577 	ids.port_id = port->d_id;
578 	ids.roles = FC_RPORT_ROLE_FCP_TARGET;
579 
580 	rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
581 	if (!rport) {
582 		dev_err(&port->adapter->ccw_device->dev,
583 			"Registering port 0x%016Lx failed\n",
584 			(unsigned long long)port->wwpn);
585 		return;
586 	}
587 
588 	rport->maxframe_size = port->maxframe_size;
589 	rport->supported_classes = port->supported_classes;
590 	port->rport = rport;
591 	port->starget_id = rport->scsi_target_id;
592 
593 	zfcp_unit_queue_scsi_scan(port);
594 }
595 
zfcp_scsi_rport_block(struct zfcp_port * port)596 static void zfcp_scsi_rport_block(struct zfcp_port *port)
597 {
598 	struct fc_rport *rport = port->rport;
599 
600 	if (rport) {
601 		fc_remote_port_delete(rport);
602 		port->rport = NULL;
603 	}
604 }
605 
zfcp_scsi_schedule_rport_register(struct zfcp_port * port)606 void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
607 {
608 	get_device(&port->dev);
609 	port->rport_task = RPORT_ADD;
610 
611 	if (!queue_work(port->adapter->work_queue, &port->rport_work))
612 		put_device(&port->dev);
613 }
614 
zfcp_scsi_schedule_rport_block(struct zfcp_port * port)615 void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
616 {
617 	get_device(&port->dev);
618 	port->rport_task = RPORT_DEL;
619 
620 	if (port->rport && queue_work(port->adapter->work_queue,
621 				      &port->rport_work))
622 		return;
623 
624 	put_device(&port->dev);
625 }
626 
zfcp_scsi_schedule_rports_block(struct zfcp_adapter * adapter)627 void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
628 {
629 	unsigned long flags;
630 	struct zfcp_port *port;
631 
632 	read_lock_irqsave(&adapter->port_list_lock, flags);
633 	list_for_each_entry(port, &adapter->port_list, list)
634 		zfcp_scsi_schedule_rport_block(port);
635 	read_unlock_irqrestore(&adapter->port_list_lock, flags);
636 }
637 
zfcp_scsi_rport_work(struct work_struct * work)638 void zfcp_scsi_rport_work(struct work_struct *work)
639 {
640 	struct zfcp_port *port = container_of(work, struct zfcp_port,
641 					      rport_work);
642 
643 	while (port->rport_task) {
644 		if (port->rport_task == RPORT_ADD) {
645 			port->rport_task = RPORT_NONE;
646 			zfcp_scsi_rport_register(port);
647 		} else {
648 			port->rport_task = RPORT_NONE;
649 			zfcp_scsi_rport_block(port);
650 		}
651 	}
652 
653 	put_device(&port->dev);
654 }
655 
656 /**
657  * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
658  * @adapter: The adapter where to configure DIF/DIX for the SCSI host
659  */
zfcp_scsi_set_prot(struct zfcp_adapter * adapter)660 void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
661 {
662 	unsigned int mask = 0;
663 	unsigned int data_div;
664 	struct Scsi_Host *shost = adapter->scsi_host;
665 
666 	data_div = atomic_read(&adapter->status) &
667 		   ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
668 
669 	if (enable_dif &&
670 	    adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
671 		mask |= SHOST_DIF_TYPE1_PROTECTION;
672 
673 	if (enable_dif && data_div &&
674 	    adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
675 		mask |= SHOST_DIX_TYPE1_PROTECTION;
676 		scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
677 		shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
678 		shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
679 		shost->max_sectors = shost->sg_tablesize * 8;
680 	}
681 
682 	scsi_host_set_prot(shost, mask);
683 }
684 
685 /**
686  * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
687  * @scmd: The SCSI command to report the error for
688  * @ascq: The ASCQ to put in the sense buffer
689  *
690  * See the error handling in sd_done for the sense codes used here.
691  * Set DID_SOFT_ERROR to retry the request, if possible.
692  */
zfcp_scsi_dif_sense_error(struct scsi_cmnd * scmd,int ascq)693 void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
694 {
695 	scsi_build_sense_buffer(1, scmd->sense_buffer,
696 				ILLEGAL_REQUEST, 0x10, ascq);
697 	set_driver_byte(scmd, DRIVER_SENSE);
698 	scmd->result |= SAM_STAT_CHECK_CONDITION;
699 	set_host_byte(scmd, DID_SOFT_ERROR);
700 }
701 
702 struct fc_function_template zfcp_transport_functions = {
703 	.show_starget_port_id = 1,
704 	.show_starget_port_name = 1,
705 	.show_starget_node_name = 1,
706 	.show_rport_supported_classes = 1,
707 	.show_rport_maxframe_size = 1,
708 	.show_rport_dev_loss_tmo = 1,
709 	.show_host_node_name = 1,
710 	.show_host_port_name = 1,
711 	.show_host_permanent_port_name = 1,
712 	.show_host_supported_classes = 1,
713 	.show_host_supported_fc4s = 1,
714 	.show_host_supported_speeds = 1,
715 	.show_host_maxframe_size = 1,
716 	.show_host_serial_number = 1,
717 	.get_fc_host_stats = zfcp_get_fc_host_stats,
718 	.reset_fc_host_stats = zfcp_reset_fc_host_stats,
719 	.set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
720 	.get_host_port_state = zfcp_get_host_port_state,
721 	.terminate_rport_io = zfcp_scsi_terminate_rport_io,
722 	.show_host_port_state = 1,
723 	.show_host_active_fc4s = 1,
724 	.bsg_request = zfcp_fc_exec_bsg_job,
725 	.bsg_timeout = zfcp_fc_timeout_bsg_job,
726 	/* no functions registered for following dynamic attributes but
727 	   directly set by LLDD */
728 	.show_host_port_type = 1,
729 	.show_host_symbolic_name = 1,
730 	.show_host_speed = 1,
731 	.show_host_port_id = 1,
732 	.dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
733 };
734