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