1 // SPDX-License-Identifier: GPL-2.0
5 * Interface to Linux SCSI midlayer.
7 * Copyright IBM Corp. 2002, 2018
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <scsi/fc/fc_fcp.h>
17 #include <scsi/scsi_eh.h>
18 #include <linux/atomic.h>
22 #include "zfcp_reqlist.h"
24 static unsigned int default_depth
= 32;
25 module_param_named(queue_depth
, default_depth
, uint
, 0600);
26 MODULE_PARM_DESC(queue_depth
, "Default queue depth for new SCSI devices");
28 static bool enable_dif
;
29 module_param_named(dif
, enable_dif
, bool, 0400);
30 MODULE_PARM_DESC(dif
, "Enable DIF data integrity support (default off)");
32 bool zfcp_experimental_dix
;
33 module_param_named(dix
, zfcp_experimental_dix
, bool, 0400);
34 MODULE_PARM_DESC(dix
, "Enable experimental DIX (data integrity extension) support which implies DIF support (default off)");
36 static bool allow_lun_scan
= true;
37 module_param(allow_lun_scan
, bool, 0600);
38 MODULE_PARM_DESC(allow_lun_scan
, "For NPIV, scan and attach all storage LUNs");
40 static void zfcp_scsi_slave_destroy(struct scsi_device
*sdev
)
42 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
44 /* if previous slave_alloc returned early, there is nothing to do */
48 zfcp_erp_lun_shutdown_wait(sdev
, "scssd_1");
49 put_device(&zfcp_sdev
->port
->dev
);
52 static int zfcp_scsi_slave_configure(struct scsi_device
*sdp
)
54 if (sdp
->tagged_supported
)
55 scsi_change_queue_depth(sdp
, default_depth
);
59 static void zfcp_scsi_command_fail(struct scsi_cmnd
*scpnt
, int result
)
61 set_host_byte(scpnt
, result
);
62 zfcp_dbf_scsi_fail_send(scpnt
);
63 scpnt
->scsi_done(scpnt
);
67 int zfcp_scsi_queuecommand(struct Scsi_Host
*shost
, struct scsi_cmnd
*scpnt
)
69 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(scpnt
->device
);
70 struct fc_rport
*rport
= starget_to_rport(scsi_target(scpnt
->device
));
71 int status
, scsi_result
, ret
;
73 /* reset the status for this request */
75 scpnt
->host_scribble
= NULL
;
77 scsi_result
= fc_remote_port_chkready(rport
);
78 if (unlikely(scsi_result
)) {
79 scpnt
->result
= scsi_result
;
80 zfcp_dbf_scsi_fail_send(scpnt
);
81 scpnt
->scsi_done(scpnt
);
85 status
= atomic_read(&zfcp_sdev
->status
);
86 if (unlikely(status
& ZFCP_STATUS_COMMON_ERP_FAILED
) &&
87 !(atomic_read(&zfcp_sdev
->port
->status
) &
88 ZFCP_STATUS_COMMON_ERP_FAILED
)) {
89 /* only LUN access denied, but port is good
90 * not covered by FC transport, have to fail here */
91 zfcp_scsi_command_fail(scpnt
, DID_ERROR
);
95 if (unlikely(!(status
& ZFCP_STATUS_COMMON_UNBLOCKED
))) {
97 * call to rport_delete pending: mimic retry from
98 * fc_remote_port_chkready until rport is BLOCKED
100 zfcp_scsi_command_fail(scpnt
, DID_IMM_RETRY
);
104 ret
= zfcp_fsf_fcp_cmnd(scpnt
);
105 if (unlikely(ret
== -EBUSY
))
106 return SCSI_MLQUEUE_DEVICE_BUSY
;
107 else if (unlikely(ret
< 0))
108 return SCSI_MLQUEUE_HOST_BUSY
;
113 static int zfcp_scsi_slave_alloc(struct scsi_device
*sdev
)
115 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
116 struct zfcp_adapter
*adapter
=
117 (struct zfcp_adapter
*) sdev
->host
->hostdata
[0];
118 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
119 struct zfcp_port
*port
;
120 struct zfcp_unit
*unit
;
121 int npiv
= adapter
->connection_features
& FSF_FEATURE_NPIV_MODE
;
123 zfcp_sdev
->erp_action
.adapter
= adapter
;
124 zfcp_sdev
->erp_action
.sdev
= sdev
;
126 port
= zfcp_get_port_by_wwpn(adapter
, rport
->port_name
);
130 zfcp_sdev
->erp_action
.port
= port
;
132 mutex_lock(&zfcp_sysfs_port_units_mutex
);
133 if (zfcp_sysfs_port_is_removing(port
)) {
134 /* port is already gone */
135 mutex_unlock(&zfcp_sysfs_port_units_mutex
);
136 put_device(&port
->dev
); /* undo zfcp_get_port_by_wwpn() */
139 mutex_unlock(&zfcp_sysfs_port_units_mutex
);
141 unit
= zfcp_unit_find(port
, zfcp_scsi_dev_lun(sdev
));
143 put_device(&unit
->dev
);
145 if (!unit
&& !(allow_lun_scan
&& npiv
)) {
146 put_device(&port
->dev
);
150 zfcp_sdev
->port
= port
;
151 zfcp_sdev
->latencies
.write
.channel
.min
= 0xFFFFFFFF;
152 zfcp_sdev
->latencies
.write
.fabric
.min
= 0xFFFFFFFF;
153 zfcp_sdev
->latencies
.read
.channel
.min
= 0xFFFFFFFF;
154 zfcp_sdev
->latencies
.read
.fabric
.min
= 0xFFFFFFFF;
155 zfcp_sdev
->latencies
.cmd
.channel
.min
= 0xFFFFFFFF;
156 zfcp_sdev
->latencies
.cmd
.fabric
.min
= 0xFFFFFFFF;
157 spin_lock_init(&zfcp_sdev
->latencies
.lock
);
159 zfcp_erp_set_lun_status(sdev
, ZFCP_STATUS_COMMON_RUNNING
);
160 zfcp_erp_lun_reopen(sdev
, 0, "scsla_1");
161 zfcp_erp_wait(port
->adapter
);
166 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd
*scpnt
)
168 struct Scsi_Host
*scsi_host
= scpnt
->device
->host
;
169 struct zfcp_adapter
*adapter
=
170 (struct zfcp_adapter
*) scsi_host
->hostdata
[0];
171 struct zfcp_fsf_req
*old_req
, *abrt_req
;
173 unsigned long old_reqid
= (unsigned long) scpnt
->host_scribble
;
174 int retval
= SUCCESS
, ret
;
178 /* avoid race condition between late normal completion and abort */
179 write_lock_irqsave(&adapter
->abort_lock
, flags
);
181 old_req
= zfcp_reqlist_find(adapter
->req_list
, old_reqid
);
183 write_unlock_irqrestore(&adapter
->abort_lock
, flags
);
184 zfcp_dbf_scsi_abort("abrt_or", scpnt
, NULL
);
185 return FAILED
; /* completion could be in progress */
187 old_req
->data
= NULL
;
189 /* don't access old fsf_req after releasing the abort_lock */
190 write_unlock_irqrestore(&adapter
->abort_lock
, flags
);
193 abrt_req
= zfcp_fsf_abort_fcp_cmnd(scpnt
);
197 zfcp_dbf_scsi_abort("abrt_wt", scpnt
, NULL
);
198 zfcp_erp_wait(adapter
);
199 ret
= fc_block_scsi_eh(scpnt
);
201 zfcp_dbf_scsi_abort("abrt_bl", scpnt
, NULL
);
204 if (!(atomic_read(&adapter
->status
) &
205 ZFCP_STATUS_COMMON_RUNNING
)) {
206 zfcp_dbf_scsi_abort("abrt_ru", scpnt
, NULL
);
211 zfcp_dbf_scsi_abort("abrt_ar", scpnt
, NULL
);
215 wait_for_completion(&abrt_req
->completion
);
217 if (abrt_req
->status
& ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED
)
219 else if (abrt_req
->status
& ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED
)
225 zfcp_dbf_scsi_abort(dbf_tag
, scpnt
, abrt_req
);
226 zfcp_fsf_req_free(abrt_req
);
230 struct zfcp_scsi_req_filter
{
236 static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req
*old_req
, void *data
)
238 struct zfcp_scsi_req_filter
*filter
=
239 (struct zfcp_scsi_req_filter
*)data
;
241 /* already aborted - prevent side-effects - or not a SCSI command */
242 if (old_req
->data
== NULL
||
243 zfcp_fsf_req_is_status_read_buffer(old_req
) ||
244 old_req
->qtcb
->header
.fsf_command
!= FSF_QTCB_FCP_CMND
)
247 /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
248 if (old_req
->qtcb
->header
.port_handle
!= filter
->port_handle
)
251 if (filter
->tmf_scope
== FCP_TMF_LUN_RESET
&&
252 old_req
->qtcb
->header
.lun_handle
!= filter
->lun_handle
)
255 zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd
*)old_req
->data
, old_req
);
256 old_req
->data
= NULL
;
259 static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev
*zsdev
, u8 tm_flags
)
261 struct zfcp_adapter
*adapter
= zsdev
->port
->adapter
;
262 struct zfcp_scsi_req_filter filter
= {
263 .tmf_scope
= FCP_TMF_TGT_RESET
,
264 .port_handle
= zsdev
->port
->handle
,
268 if (tm_flags
== FCP_TMF_LUN_RESET
) {
269 filter
.tmf_scope
= FCP_TMF_LUN_RESET
;
270 filter
.lun_handle
= zsdev
->lun_handle
;
274 * abort_lock secures against other processings - in the abort-function
275 * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
277 write_lock_irqsave(&adapter
->abort_lock
, flags
);
278 zfcp_reqlist_apply_for_all(adapter
->req_list
, zfcp_scsi_forget_cmnd
,
280 write_unlock_irqrestore(&adapter
->abort_lock
, flags
);
284 * zfcp_scsi_task_mgmt_function() - Send a task management function (sync).
285 * @sdev: Pointer to SCSI device to send the task management command to.
286 * @tm_flags: Task management flags,
287 * here we only handle %FCP_TMF_TGT_RESET or %FCP_TMF_LUN_RESET.
289 static int zfcp_scsi_task_mgmt_function(struct scsi_device
*sdev
, u8 tm_flags
)
291 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(sdev
);
292 struct zfcp_adapter
*adapter
= zfcp_sdev
->port
->adapter
;
293 struct fc_rport
*rport
= starget_to_rport(scsi_target(sdev
));
294 struct zfcp_fsf_req
*fsf_req
= NULL
;
295 int retval
= SUCCESS
, ret
;
299 fsf_req
= zfcp_fsf_fcp_task_mgmt(sdev
, tm_flags
);
303 zfcp_dbf_scsi_devreset("wait", sdev
, tm_flags
, NULL
);
304 zfcp_erp_wait(adapter
);
305 ret
= fc_block_rport(rport
);
307 zfcp_dbf_scsi_devreset("fiof", sdev
, tm_flags
, NULL
);
311 if (!(atomic_read(&adapter
->status
) &
312 ZFCP_STATUS_COMMON_RUNNING
)) {
313 zfcp_dbf_scsi_devreset("nres", sdev
, tm_flags
, NULL
);
318 zfcp_dbf_scsi_devreset("reqf", sdev
, tm_flags
, NULL
);
322 wait_for_completion(&fsf_req
->completion
);
324 if (fsf_req
->status
& ZFCP_STATUS_FSFREQ_TMFUNCFAILED
) {
325 zfcp_dbf_scsi_devreset("fail", sdev
, tm_flags
, fsf_req
);
328 zfcp_dbf_scsi_devreset("okay", sdev
, tm_flags
, fsf_req
);
329 zfcp_scsi_forget_cmnds(zfcp_sdev
, tm_flags
);
332 zfcp_fsf_req_free(fsf_req
);
336 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd
*scpnt
)
338 struct scsi_device
*sdev
= scpnt
->device
;
340 return zfcp_scsi_task_mgmt_function(sdev
, FCP_TMF_LUN_RESET
);
343 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd
*scpnt
)
345 struct scsi_target
*starget
= scsi_target(scpnt
->device
);
346 struct fc_rport
*rport
= starget_to_rport(starget
);
347 struct Scsi_Host
*shost
= rport_to_shost(rport
);
348 struct scsi_device
*sdev
= NULL
, *tmp_sdev
;
349 struct zfcp_adapter
*adapter
=
350 (struct zfcp_adapter
*)shost
->hostdata
[0];
353 shost_for_each_device(tmp_sdev
, shost
) {
354 if (tmp_sdev
->id
== starget
->id
) {
361 zfcp_dbf_scsi_eh("tr_nosd", adapter
, starget
->id
, ret
);
365 ret
= zfcp_scsi_task_mgmt_function(sdev
, FCP_TMF_TGT_RESET
);
367 /* release reference from above shost_for_each_device */
369 scsi_device_put(tmp_sdev
);
374 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd
*scpnt
)
376 struct zfcp_scsi_dev
*zfcp_sdev
= sdev_to_zfcp(scpnt
->device
);
377 struct zfcp_adapter
*adapter
= zfcp_sdev
->port
->adapter
;
378 int ret
= SUCCESS
, fc_ret
;
380 if (!(adapter
->connection_features
& FSF_FEATURE_NPIV_MODE
)) {
381 zfcp_erp_port_forced_reopen_all(adapter
, 0, "schrh_p");
382 zfcp_erp_wait(adapter
);
384 zfcp_erp_adapter_reopen(adapter
, 0, "schrh_1");
385 zfcp_erp_wait(adapter
);
386 fc_ret
= fc_block_scsi_eh(scpnt
);
390 zfcp_dbf_scsi_eh("schrh_r", adapter
, ~0, ret
);
395 * zfcp_scsi_sysfs_host_reset() - Support scsi_host sysfs attribute host_reset.
396 * @shost: Pointer to Scsi_Host to perform action on.
397 * @reset_type: We support %SCSI_ADAPTER_RESET but not %SCSI_FIRMWARE_RESET.
399 * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise.
401 * This is similar to zfcp_sysfs_adapter_failed_store().
403 static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host
*shost
, int reset_type
)
405 struct zfcp_adapter
*adapter
=
406 (struct zfcp_adapter
*)shost
->hostdata
[0];
409 if (reset_type
!= SCSI_ADAPTER_RESET
) {
411 zfcp_dbf_scsi_eh("scshr_n", adapter
, ~0, ret
);
415 zfcp_erp_adapter_reset_sync(adapter
, "scshr_y");
419 struct scsi_transport_template
*zfcp_scsi_transport_template
;
421 static struct scsi_host_template zfcp_scsi_host_template
= {
422 .module
= THIS_MODULE
,
424 .queuecommand
= zfcp_scsi_queuecommand
,
425 .eh_timed_out
= fc_eh_timed_out
,
426 .eh_abort_handler
= zfcp_scsi_eh_abort_handler
,
427 .eh_device_reset_handler
= zfcp_scsi_eh_device_reset_handler
,
428 .eh_target_reset_handler
= zfcp_scsi_eh_target_reset_handler
,
429 .eh_host_reset_handler
= zfcp_scsi_eh_host_reset_handler
,
430 .slave_alloc
= zfcp_scsi_slave_alloc
,
431 .slave_configure
= zfcp_scsi_slave_configure
,
432 .slave_destroy
= zfcp_scsi_slave_destroy
,
433 .change_queue_depth
= scsi_change_queue_depth
,
434 .host_reset
= zfcp_scsi_sysfs_host_reset
,
438 .sg_tablesize
= (((QDIO_MAX_ELEMENTS_PER_BUFFER
- 1)
439 * ZFCP_QDIO_MAX_SBALS_PER_REQ
) - 2),
440 /* GCD, adjusted later */
441 .max_sectors
= (((QDIO_MAX_ELEMENTS_PER_BUFFER
- 1)
442 * ZFCP_QDIO_MAX_SBALS_PER_REQ
) - 2) * 8,
443 /* GCD, adjusted later */
444 /* report size limit per scatter-gather segment */
445 .max_segment_size
= ZFCP_QDIO_SBALE_LEN
,
446 .dma_boundary
= ZFCP_QDIO_SBALE_LEN
- 1,
447 .shost_attrs
= zfcp_sysfs_shost_attrs
,
448 .sdev_attrs
= zfcp_sysfs_sdev_attrs
,
449 .track_queue_depth
= 1,
450 .supported_mode
= MODE_INITIATOR
,
454 * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
455 * @adapter: The zfcp adapter to register with the SCSI midlayer
457 int zfcp_scsi_adapter_register(struct zfcp_adapter
*adapter
)
459 struct ccw_dev_id dev_id
;
461 if (adapter
->scsi_host
)
464 ccw_device_get_id(adapter
->ccw_device
, &dev_id
);
465 /* register adapter as SCSI host with mid layer of SCSI stack */
466 adapter
->scsi_host
= scsi_host_alloc(&zfcp_scsi_host_template
,
467 sizeof (struct zfcp_adapter
*));
468 if (!adapter
->scsi_host
) {
469 dev_err(&adapter
->ccw_device
->dev
,
470 "Registering the FCP device with the "
471 "SCSI stack failed\n");
475 /* tell the SCSI stack some characteristics of this adapter */
476 adapter
->scsi_host
->max_id
= 511;
477 adapter
->scsi_host
->max_lun
= 0xFFFFFFFF;
478 adapter
->scsi_host
->max_channel
= 0;
479 adapter
->scsi_host
->unique_id
= dev_id
.devno
;
480 adapter
->scsi_host
->max_cmd_len
= 16; /* in struct fcp_cmnd */
481 adapter
->scsi_host
->transportt
= zfcp_scsi_transport_template
;
483 adapter
->scsi_host
->hostdata
[0] = (unsigned long) adapter
;
485 if (scsi_add_host(adapter
->scsi_host
, &adapter
->ccw_device
->dev
)) {
486 scsi_host_put(adapter
->scsi_host
);
494 * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
495 * @adapter: The zfcp adapter to unregister.
497 void zfcp_scsi_adapter_unregister(struct zfcp_adapter
*adapter
)
499 struct Scsi_Host
*shost
;
500 struct zfcp_port
*port
;
502 shost
= adapter
->scsi_host
;
506 read_lock_irq(&adapter
->port_list_lock
);
507 list_for_each_entry(port
, &adapter
->port_list
, list
)
509 read_unlock_irq(&adapter
->port_list_lock
);
511 fc_remove_host(shost
);
512 scsi_remove_host(shost
);
513 scsi_host_put(shost
);
514 adapter
->scsi_host
= NULL
;
517 static struct fc_host_statistics
*
518 zfcp_scsi_init_fc_host_stats(struct zfcp_adapter
*adapter
)
520 struct fc_host_statistics
*fc_stats
;
522 if (!adapter
->fc_stats
) {
523 fc_stats
= kmalloc(sizeof(*fc_stats
), GFP_KERNEL
);
526 adapter
->fc_stats
= fc_stats
; /* freed in adapter_release */
528 memset(adapter
->fc_stats
, 0, sizeof(*adapter
->fc_stats
));
529 return adapter
->fc_stats
;
532 static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics
*fc_stats
,
533 struct fsf_qtcb_bottom_port
*data
,
534 struct fsf_qtcb_bottom_port
*old
)
536 fc_stats
->seconds_since_last_reset
=
537 data
->seconds_since_last_reset
- old
->seconds_since_last_reset
;
538 fc_stats
->tx_frames
= data
->tx_frames
- old
->tx_frames
;
539 fc_stats
->tx_words
= data
->tx_words
- old
->tx_words
;
540 fc_stats
->rx_frames
= data
->rx_frames
- old
->rx_frames
;
541 fc_stats
->rx_words
= data
->rx_words
- old
->rx_words
;
542 fc_stats
->lip_count
= data
->lip
- old
->lip
;
543 fc_stats
->nos_count
= data
->nos
- old
->nos
;
544 fc_stats
->error_frames
= data
->error_frames
- old
->error_frames
;
545 fc_stats
->dumped_frames
= data
->dumped_frames
- old
->dumped_frames
;
546 fc_stats
->link_failure_count
= data
->link_failure
- old
->link_failure
;
547 fc_stats
->loss_of_sync_count
= data
->loss_of_sync
- old
->loss_of_sync
;
548 fc_stats
->loss_of_signal_count
=
549 data
->loss_of_signal
- old
->loss_of_signal
;
550 fc_stats
->prim_seq_protocol_err_count
=
551 data
->psp_error_counts
- old
->psp_error_counts
;
552 fc_stats
->invalid_tx_word_count
=
553 data
->invalid_tx_words
- old
->invalid_tx_words
;
554 fc_stats
->invalid_crc_count
= data
->invalid_crcs
- old
->invalid_crcs
;
555 fc_stats
->fcp_input_requests
=
556 data
->input_requests
- old
->input_requests
;
557 fc_stats
->fcp_output_requests
=
558 data
->output_requests
- old
->output_requests
;
559 fc_stats
->fcp_control_requests
=
560 data
->control_requests
- old
->control_requests
;
561 fc_stats
->fcp_input_megabytes
= data
->input_mb
- old
->input_mb
;
562 fc_stats
->fcp_output_megabytes
= data
->output_mb
- old
->output_mb
;
565 static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics
*fc_stats
,
566 struct fsf_qtcb_bottom_port
*data
)
568 fc_stats
->seconds_since_last_reset
= data
->seconds_since_last_reset
;
569 fc_stats
->tx_frames
= data
->tx_frames
;
570 fc_stats
->tx_words
= data
->tx_words
;
571 fc_stats
->rx_frames
= data
->rx_frames
;
572 fc_stats
->rx_words
= data
->rx_words
;
573 fc_stats
->lip_count
= data
->lip
;
574 fc_stats
->nos_count
= data
->nos
;
575 fc_stats
->error_frames
= data
->error_frames
;
576 fc_stats
->dumped_frames
= data
->dumped_frames
;
577 fc_stats
->link_failure_count
= data
->link_failure
;
578 fc_stats
->loss_of_sync_count
= data
->loss_of_sync
;
579 fc_stats
->loss_of_signal_count
= data
->loss_of_signal
;
580 fc_stats
->prim_seq_protocol_err_count
= data
->psp_error_counts
;
581 fc_stats
->invalid_tx_word_count
= data
->invalid_tx_words
;
582 fc_stats
->invalid_crc_count
= data
->invalid_crcs
;
583 fc_stats
->fcp_input_requests
= data
->input_requests
;
584 fc_stats
->fcp_output_requests
= data
->output_requests
;
585 fc_stats
->fcp_control_requests
= data
->control_requests
;
586 fc_stats
->fcp_input_megabytes
= data
->input_mb
;
587 fc_stats
->fcp_output_megabytes
= data
->output_mb
;
590 static struct fc_host_statistics
*
591 zfcp_scsi_get_fc_host_stats(struct Scsi_Host
*host
)
593 struct zfcp_adapter
*adapter
;
594 struct fc_host_statistics
*fc_stats
;
595 struct fsf_qtcb_bottom_port
*data
;
598 adapter
= (struct zfcp_adapter
*)host
->hostdata
[0];
599 fc_stats
= zfcp_scsi_init_fc_host_stats(adapter
);
603 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
607 ret
= zfcp_fsf_exchange_port_data_sync(adapter
->qdio
, data
);
608 if (ret
!= 0 && ret
!= -EAGAIN
) {
613 if (adapter
->stats_reset
&&
614 ((jiffies
/HZ
- adapter
->stats_reset
) <
615 data
->seconds_since_last_reset
))
616 zfcp_scsi_adjust_fc_host_stats(fc_stats
, data
,
617 adapter
->stats_reset_data
);
619 zfcp_scsi_set_fc_host_stats(fc_stats
, data
);
625 static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host
*shost
)
627 struct zfcp_adapter
*adapter
;
628 struct fsf_qtcb_bottom_port
*data
;
631 adapter
= (struct zfcp_adapter
*)shost
->hostdata
[0];
632 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
636 ret
= zfcp_fsf_exchange_port_data_sync(adapter
->qdio
, data
);
637 if (ret
!= 0 && ret
!= -EAGAIN
)
640 adapter
->stats_reset
= jiffies
/HZ
;
641 kfree(adapter
->stats_reset_data
);
642 adapter
->stats_reset_data
= data
; /* finally freed in
647 static void zfcp_scsi_get_host_port_state(struct Scsi_Host
*shost
)
649 struct zfcp_adapter
*adapter
=
650 (struct zfcp_adapter
*)shost
->hostdata
[0];
651 int status
= atomic_read(&adapter
->status
);
653 if ((status
& ZFCP_STATUS_COMMON_RUNNING
) &&
654 !(status
& ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
))
655 fc_host_port_state(shost
) = FC_PORTSTATE_ONLINE
;
656 else if (status
& ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED
)
657 fc_host_port_state(shost
) = FC_PORTSTATE_LINKDOWN
;
658 else if (status
& ZFCP_STATUS_COMMON_ERP_FAILED
)
659 fc_host_port_state(shost
) = FC_PORTSTATE_ERROR
;
661 fc_host_port_state(shost
) = FC_PORTSTATE_UNKNOWN
;
664 static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport
*rport
,
667 rport
->dev_loss_tmo
= timeout
;
671 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
672 * @rport: The FC rport where to teminate I/O
674 * Abort all pending SCSI commands for a port by closing the
675 * port. Using a reopen avoids a conflict with a shutdown
676 * overwriting a reopen. The "forced" ensures that a disappeared port
677 * is not opened again as valid due to the cached plogi data in
680 static void zfcp_scsi_terminate_rport_io(struct fc_rport
*rport
)
682 struct zfcp_port
*port
;
683 struct Scsi_Host
*shost
= rport_to_shost(rport
);
684 struct zfcp_adapter
*adapter
=
685 (struct zfcp_adapter
*)shost
->hostdata
[0];
687 port
= zfcp_get_port_by_wwpn(adapter
, rport
->port_name
);
690 zfcp_erp_port_forced_reopen(port
, 0, "sctrpi1");
691 put_device(&port
->dev
);
693 zfcp_erp_port_forced_no_port_dbf(
695 rport
->port_name
/* zfcp_scsi_rport_register */,
696 rport
->port_id
/* zfcp_scsi_rport_register */);
700 static void zfcp_scsi_rport_register(struct zfcp_port
*port
)
702 struct fc_rport_identifiers ids
;
703 struct fc_rport
*rport
;
708 ids
.node_name
= port
->wwnn
;
709 ids
.port_name
= port
->wwpn
;
710 ids
.port_id
= port
->d_id
;
711 ids
.roles
= FC_RPORT_ROLE_FCP_TARGET
;
713 zfcp_dbf_rec_trig_lock("scpaddy", port
->adapter
, port
, NULL
,
714 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD
,
715 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD
);
716 rport
= fc_remote_port_add(port
->adapter
->scsi_host
, 0, &ids
);
718 dev_err(&port
->adapter
->ccw_device
->dev
,
719 "Registering port 0x%016Lx failed\n",
720 (unsigned long long)port
->wwpn
);
724 rport
->maxframe_size
= port
->maxframe_size
;
725 rport
->supported_classes
= port
->supported_classes
;
727 port
->starget_id
= rport
->scsi_target_id
;
729 zfcp_unit_queue_scsi_scan(port
);
732 static void zfcp_scsi_rport_block(struct zfcp_port
*port
)
734 struct fc_rport
*rport
= port
->rport
;
737 zfcp_dbf_rec_trig_lock("scpdely", port
->adapter
, port
, NULL
,
738 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL
,
739 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL
);
740 fc_remote_port_delete(rport
);
745 void zfcp_scsi_schedule_rport_register(struct zfcp_port
*port
)
747 get_device(&port
->dev
);
748 port
->rport_task
= RPORT_ADD
;
750 if (!queue_work(port
->adapter
->work_queue
, &port
->rport_work
))
751 put_device(&port
->dev
);
754 void zfcp_scsi_schedule_rport_block(struct zfcp_port
*port
)
756 get_device(&port
->dev
);
757 port
->rport_task
= RPORT_DEL
;
759 if (port
->rport
&& queue_work(port
->adapter
->work_queue
,
763 put_device(&port
->dev
);
766 void zfcp_scsi_schedule_rports_block(struct zfcp_adapter
*adapter
)
769 struct zfcp_port
*port
;
771 read_lock_irqsave(&adapter
->port_list_lock
, flags
);
772 list_for_each_entry(port
, &adapter
->port_list
, list
)
773 zfcp_scsi_schedule_rport_block(port
);
774 read_unlock_irqrestore(&adapter
->port_list_lock
, flags
);
777 void zfcp_scsi_rport_work(struct work_struct
*work
)
779 struct zfcp_port
*port
= container_of(work
, struct zfcp_port
,
782 set_worker_desc("zrp%c-%16llx",
783 (port
->rport_task
== RPORT_ADD
) ? 'a' : 'd',
784 port
->wwpn
); /* < WORKER_DESC_LEN=24 */
785 while (port
->rport_task
) {
786 if (port
->rport_task
== RPORT_ADD
) {
787 port
->rport_task
= RPORT_NONE
;
788 zfcp_scsi_rport_register(port
);
790 port
->rport_task
= RPORT_NONE
;
791 zfcp_scsi_rport_block(port
);
795 put_device(&port
->dev
);
799 * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
800 * @adapter: The adapter where to configure DIF/DIX for the SCSI host
802 void zfcp_scsi_set_prot(struct zfcp_adapter
*adapter
)
804 unsigned int mask
= 0;
805 unsigned int data_div
;
806 struct Scsi_Host
*shost
= adapter
->scsi_host
;
808 data_div
= atomic_read(&adapter
->status
) &
809 ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED
;
811 if ((enable_dif
|| zfcp_experimental_dix
) &&
812 adapter
->adapter_features
& FSF_FEATURE_DIF_PROT_TYPE1
)
813 mask
|= SHOST_DIF_TYPE1_PROTECTION
;
815 if (zfcp_experimental_dix
&& data_div
&&
816 adapter
->adapter_features
& FSF_FEATURE_DIX_PROT_TCPIP
) {
817 mask
|= SHOST_DIX_TYPE1_PROTECTION
;
818 scsi_host_set_guard(shost
, SHOST_DIX_GUARD_IP
);
819 shost
->sg_prot_tablesize
= adapter
->qdio
->max_sbale_per_req
/ 2;
820 shost
->sg_tablesize
= adapter
->qdio
->max_sbale_per_req
/ 2;
821 shost
->max_sectors
= shost
->sg_tablesize
* 8;
824 scsi_host_set_prot(shost
, mask
);
828 * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
829 * @scmd: The SCSI command to report the error for
830 * @ascq: The ASCQ to put in the sense buffer
832 * See the error handling in sd_done for the sense codes used here.
833 * Set DID_SOFT_ERROR to retry the request, if possible.
835 void zfcp_scsi_dif_sense_error(struct scsi_cmnd
*scmd
, int ascq
)
837 scsi_build_sense_buffer(1, scmd
->sense_buffer
,
838 ILLEGAL_REQUEST
, 0x10, ascq
);
839 set_driver_byte(scmd
, DRIVER_SENSE
);
840 scmd
->result
|= SAM_STAT_CHECK_CONDITION
;
841 set_host_byte(scmd
, DID_SOFT_ERROR
);
844 struct fc_function_template zfcp_transport_functions
= {
845 .show_starget_port_id
= 1,
846 .show_starget_port_name
= 1,
847 .show_starget_node_name
= 1,
848 .show_rport_supported_classes
= 1,
849 .show_rport_maxframe_size
= 1,
850 .show_rport_dev_loss_tmo
= 1,
851 .show_host_node_name
= 1,
852 .show_host_port_name
= 1,
853 .show_host_permanent_port_name
= 1,
854 .show_host_supported_classes
= 1,
855 .show_host_supported_fc4s
= 1,
856 .show_host_supported_speeds
= 1,
857 .show_host_maxframe_size
= 1,
858 .show_host_serial_number
= 1,
859 .get_fc_host_stats
= zfcp_scsi_get_fc_host_stats
,
860 .reset_fc_host_stats
= zfcp_scsi_reset_fc_host_stats
,
861 .set_rport_dev_loss_tmo
= zfcp_scsi_set_rport_dev_loss_tmo
,
862 .get_host_port_state
= zfcp_scsi_get_host_port_state
,
863 .terminate_rport_io
= zfcp_scsi_terminate_rport_io
,
864 .show_host_port_state
= 1,
865 .show_host_active_fc4s
= 1,
866 .bsg_request
= zfcp_fc_exec_bsg_job
,
867 .bsg_timeout
= zfcp_fc_timeout_bsg_job
,
868 /* no functions registered for following dynamic attributes but
869 directly set by LLDD */
870 .show_host_port_type
= 1,
871 .show_host_symbolic_name
= 1,
872 .show_host_speed
= 1,
873 .show_host_port_id
= 1,
874 .dd_bsg_size
= sizeof(struct zfcp_fsf_ct_els
),