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