of/platform: Initialise default DMA masks
[linux/fpc-iii.git] / drivers / s390 / scsi / zfcp_scsi.c
bloba8efcb330bc1d10c4b41898a48f1d830301ddd75
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * zfcp device driver
5 * Interface to Linux SCSI midlayer.
7 * Copyright IBM Corp. 2002, 2018
8 */
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>
19 #include "zfcp_ext.h"
20 #include "zfcp_dbf.h"
21 #include "zfcp_fc.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/DIX data integrity support");
32 static bool allow_lun_scan = true;
33 module_param(allow_lun_scan, bool, 0600);
34 MODULE_PARM_DESC(allow_lun_scan, "For NPIV, scan and attach all storage LUNs");
36 static void zfcp_scsi_slave_destroy(struct scsi_device *sdev)
38 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
40 /* if previous slave_alloc returned early, there is nothing to do */
41 if (!zfcp_sdev->port)
42 return;
44 zfcp_erp_lun_shutdown_wait(sdev, "scssd_1");
45 put_device(&zfcp_sdev->port->dev);
48 static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
50 if (sdp->tagged_supported)
51 scsi_change_queue_depth(sdp, default_depth);
52 return 0;
55 static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
57 set_host_byte(scpnt, result);
58 zfcp_dbf_scsi_fail_send(scpnt);
59 scpnt->scsi_done(scpnt);
62 static
63 int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt)
65 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
66 struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
67 int status, scsi_result, ret;
69 /* reset the status for this request */
70 scpnt->result = 0;
71 scpnt->host_scribble = NULL;
73 scsi_result = fc_remote_port_chkready(rport);
74 if (unlikely(scsi_result)) {
75 scpnt->result = scsi_result;
76 zfcp_dbf_scsi_fail_send(scpnt);
77 scpnt->scsi_done(scpnt);
78 return 0;
81 status = atomic_read(&zfcp_sdev->status);
82 if (unlikely(status & ZFCP_STATUS_COMMON_ERP_FAILED) &&
83 !(atomic_read(&zfcp_sdev->port->status) &
84 ZFCP_STATUS_COMMON_ERP_FAILED)) {
85 /* only LUN access denied, but port is good
86 * not covered by FC transport, have to fail here */
87 zfcp_scsi_command_fail(scpnt, DID_ERROR);
88 return 0;
91 if (unlikely(!(status & ZFCP_STATUS_COMMON_UNBLOCKED))) {
92 /* This could be
93 * call to rport_delete pending: mimic retry from
94 * fc_remote_port_chkready until rport is BLOCKED
96 zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY);
97 return 0;
100 ret = zfcp_fsf_fcp_cmnd(scpnt);
101 if (unlikely(ret == -EBUSY))
102 return SCSI_MLQUEUE_DEVICE_BUSY;
103 else if (unlikely(ret < 0))
104 return SCSI_MLQUEUE_HOST_BUSY;
106 return ret;
109 static int zfcp_scsi_slave_alloc(struct scsi_device *sdev)
111 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
112 struct zfcp_adapter *adapter =
113 (struct zfcp_adapter *) sdev->host->hostdata[0];
114 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
115 struct zfcp_port *port;
116 struct zfcp_unit *unit;
117 int npiv = adapter->connection_features & FSF_FEATURE_NPIV_MODE;
119 zfcp_sdev->erp_action.adapter = adapter;
120 zfcp_sdev->erp_action.sdev = sdev;
122 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
123 if (!port)
124 return -ENXIO;
126 zfcp_sdev->erp_action.port = port;
128 unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
129 if (unit)
130 put_device(&unit->dev);
132 if (!unit && !(allow_lun_scan && npiv)) {
133 put_device(&port->dev);
134 return -ENXIO;
137 zfcp_sdev->port = port;
138 zfcp_sdev->latencies.write.channel.min = 0xFFFFFFFF;
139 zfcp_sdev->latencies.write.fabric.min = 0xFFFFFFFF;
140 zfcp_sdev->latencies.read.channel.min = 0xFFFFFFFF;
141 zfcp_sdev->latencies.read.fabric.min = 0xFFFFFFFF;
142 zfcp_sdev->latencies.cmd.channel.min = 0xFFFFFFFF;
143 zfcp_sdev->latencies.cmd.fabric.min = 0xFFFFFFFF;
144 spin_lock_init(&zfcp_sdev->latencies.lock);
146 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
147 zfcp_erp_lun_reopen(sdev, 0, "scsla_1");
148 zfcp_erp_wait(port->adapter);
150 return 0;
153 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
155 struct Scsi_Host *scsi_host = scpnt->device->host;
156 struct zfcp_adapter *adapter =
157 (struct zfcp_adapter *) scsi_host->hostdata[0];
158 struct zfcp_fsf_req *old_req, *abrt_req;
159 unsigned long flags;
160 unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
161 int retval = SUCCESS, ret;
162 int retry = 3;
163 char *dbf_tag;
165 /* avoid race condition between late normal completion and abort */
166 write_lock_irqsave(&adapter->abort_lock, flags);
168 old_req = zfcp_reqlist_find(adapter->req_list, old_reqid);
169 if (!old_req) {
170 write_unlock_irqrestore(&adapter->abort_lock, flags);
171 zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL);
172 return FAILED; /* completion could be in progress */
174 old_req->data = NULL;
176 /* don't access old fsf_req after releasing the abort_lock */
177 write_unlock_irqrestore(&adapter->abort_lock, flags);
179 while (retry--) {
180 abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt);
181 if (abrt_req)
182 break;
184 zfcp_dbf_scsi_abort("abrt_wt", scpnt, NULL);
185 zfcp_erp_wait(adapter);
186 ret = fc_block_scsi_eh(scpnt);
187 if (ret) {
188 zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL);
189 return ret;
191 if (!(atomic_read(&adapter->status) &
192 ZFCP_STATUS_COMMON_RUNNING)) {
193 zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL);
194 return SUCCESS;
197 if (!abrt_req) {
198 zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL);
199 return FAILED;
202 wait_for_completion(&abrt_req->completion);
204 if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
205 dbf_tag = "abrt_ok";
206 else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
207 dbf_tag = "abrt_nn";
208 else {
209 dbf_tag = "abrt_fa";
210 retval = FAILED;
212 zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req);
213 zfcp_fsf_req_free(abrt_req);
214 return retval;
217 struct zfcp_scsi_req_filter {
218 u8 tmf_scope;
219 u32 lun_handle;
220 u32 port_handle;
223 static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
225 struct zfcp_scsi_req_filter *filter =
226 (struct zfcp_scsi_req_filter *)data;
228 /* already aborted - prevent side-effects - or not a SCSI command */
229 if (old_req->data == NULL || old_req->fsf_command != FSF_QTCB_FCP_CMND)
230 return;
232 /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
233 if (old_req->qtcb->header.port_handle != filter->port_handle)
234 return;
236 if (filter->tmf_scope == FCP_TMF_LUN_RESET &&
237 old_req->qtcb->header.lun_handle != filter->lun_handle)
238 return;
240 zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
241 old_req->data = NULL;
244 static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags)
246 struct zfcp_adapter *adapter = zsdev->port->adapter;
247 struct zfcp_scsi_req_filter filter = {
248 .tmf_scope = FCP_TMF_TGT_RESET,
249 .port_handle = zsdev->port->handle,
251 unsigned long flags;
253 if (tm_flags == FCP_TMF_LUN_RESET) {
254 filter.tmf_scope = FCP_TMF_LUN_RESET;
255 filter.lun_handle = zsdev->lun_handle;
259 * abort_lock secures against other processings - in the abort-function
260 * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
262 write_lock_irqsave(&adapter->abort_lock, flags);
263 zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd,
264 &filter);
265 write_unlock_irqrestore(&adapter->abort_lock, flags);
269 * zfcp_scsi_task_mgmt_function() - Send a task management function (sync).
270 * @sdev: Pointer to SCSI device to send the task management command to.
271 * @tm_flags: Task management flags,
272 * here we only handle %FCP_TMF_TGT_RESET or %FCP_TMF_LUN_RESET.
274 static int zfcp_scsi_task_mgmt_function(struct scsi_device *sdev, u8 tm_flags)
276 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
277 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
278 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
279 struct zfcp_fsf_req *fsf_req = NULL;
280 int retval = SUCCESS, ret;
281 int retry = 3;
283 while (retry--) {
284 fsf_req = zfcp_fsf_fcp_task_mgmt(sdev, tm_flags);
285 if (fsf_req)
286 break;
288 zfcp_dbf_scsi_devreset("wait", sdev, tm_flags, NULL);
289 zfcp_erp_wait(adapter);
290 ret = fc_block_rport(rport);
291 if (ret) {
292 zfcp_dbf_scsi_devreset("fiof", sdev, tm_flags, NULL);
293 return ret;
296 if (!(atomic_read(&adapter->status) &
297 ZFCP_STATUS_COMMON_RUNNING)) {
298 zfcp_dbf_scsi_devreset("nres", sdev, tm_flags, NULL);
299 return SUCCESS;
302 if (!fsf_req) {
303 zfcp_dbf_scsi_devreset("reqf", sdev, tm_flags, NULL);
304 return FAILED;
307 wait_for_completion(&fsf_req->completion);
309 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
310 zfcp_dbf_scsi_devreset("fail", sdev, tm_flags, fsf_req);
311 retval = FAILED;
312 } else {
313 zfcp_dbf_scsi_devreset("okay", sdev, tm_flags, fsf_req);
314 zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags);
317 zfcp_fsf_req_free(fsf_req);
318 return retval;
321 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
323 struct scsi_device *sdev = scpnt->device;
325 return zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_LUN_RESET);
328 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
330 struct scsi_target *starget = scsi_target(scpnt->device);
331 struct fc_rport *rport = starget_to_rport(starget);
332 struct Scsi_Host *shost = rport_to_shost(rport);
333 struct scsi_device *sdev = NULL, *tmp_sdev;
334 struct zfcp_adapter *adapter =
335 (struct zfcp_adapter *)shost->hostdata[0];
336 int ret;
338 shost_for_each_device(tmp_sdev, shost) {
339 if (tmp_sdev->id == starget->id) {
340 sdev = tmp_sdev;
341 break;
344 if (!sdev) {
345 ret = FAILED;
346 zfcp_dbf_scsi_eh("tr_nosd", adapter, starget->id, ret);
347 return ret;
350 ret = zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_TGT_RESET);
352 /* release reference from above shost_for_each_device */
353 if (sdev)
354 scsi_device_put(tmp_sdev);
356 return ret;
359 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
361 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
362 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
363 int ret = SUCCESS, fc_ret;
365 zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
366 zfcp_erp_wait(adapter);
367 fc_ret = fc_block_scsi_eh(scpnt);
368 if (fc_ret)
369 ret = fc_ret;
371 zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
372 return ret;
376 * zfcp_scsi_sysfs_host_reset() - Support scsi_host sysfs attribute host_reset.
377 * @shost: Pointer to Scsi_Host to perform action on.
378 * @reset_type: We support %SCSI_ADAPTER_RESET but not %SCSI_FIRMWARE_RESET.
380 * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise.
382 * This is similar to zfcp_sysfs_adapter_failed_store().
384 static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host *shost, int reset_type)
386 struct zfcp_adapter *adapter =
387 (struct zfcp_adapter *)shost->hostdata[0];
388 int ret = 0;
390 if (reset_type != SCSI_ADAPTER_RESET) {
391 ret = -EOPNOTSUPP;
392 zfcp_dbf_scsi_eh("scshr_n", adapter, ~0, ret);
393 return ret;
396 zfcp_erp_adapter_reset_sync(adapter, "scshr_y");
397 return ret;
400 struct scsi_transport_template *zfcp_scsi_transport_template;
402 static struct scsi_host_template zfcp_scsi_host_template = {
403 .module = THIS_MODULE,
404 .name = "zfcp",
405 .queuecommand = zfcp_scsi_queuecommand,
406 .eh_timed_out = fc_eh_timed_out,
407 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
408 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
409 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
410 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
411 .slave_alloc = zfcp_scsi_slave_alloc,
412 .slave_configure = zfcp_scsi_slave_configure,
413 .slave_destroy = zfcp_scsi_slave_destroy,
414 .change_queue_depth = scsi_change_queue_depth,
415 .host_reset = zfcp_scsi_sysfs_host_reset,
416 .proc_name = "zfcp",
417 .can_queue = 4096,
418 .this_id = -1,
419 .sg_tablesize = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
420 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
421 /* GCD, adjusted later */
422 .max_sectors = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
423 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
424 /* GCD, adjusted later */
425 .dma_boundary = ZFCP_QDIO_SBALE_LEN - 1,
426 .use_clustering = 1,
427 .shost_attrs = zfcp_sysfs_shost_attrs,
428 .sdev_attrs = zfcp_sysfs_sdev_attrs,
429 .track_queue_depth = 1,
430 .supported_mode = MODE_INITIATOR,
434 * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
435 * @adapter: The zfcp adapter to register with the SCSI midlayer
437 int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
439 struct ccw_dev_id dev_id;
441 if (adapter->scsi_host)
442 return 0;
444 ccw_device_get_id(adapter->ccw_device, &dev_id);
445 /* register adapter as SCSI host with mid layer of SCSI stack */
446 adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
447 sizeof (struct zfcp_adapter *));
448 if (!adapter->scsi_host) {
449 dev_err(&adapter->ccw_device->dev,
450 "Registering the FCP device with the "
451 "SCSI stack failed\n");
452 return -EIO;
455 /* tell the SCSI stack some characteristics of this adapter */
456 adapter->scsi_host->max_id = 511;
457 adapter->scsi_host->max_lun = 0xFFFFFFFF;
458 adapter->scsi_host->max_channel = 0;
459 adapter->scsi_host->unique_id = dev_id.devno;
460 adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
461 adapter->scsi_host->transportt = zfcp_scsi_transport_template;
463 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
465 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
466 scsi_host_put(adapter->scsi_host);
467 return -EIO;
470 return 0;
474 * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
475 * @adapter: The zfcp adapter to unregister.
477 void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
479 struct Scsi_Host *shost;
480 struct zfcp_port *port;
482 shost = adapter->scsi_host;
483 if (!shost)
484 return;
486 read_lock_irq(&adapter->port_list_lock);
487 list_for_each_entry(port, &adapter->port_list, list)
488 port->rport = NULL;
489 read_unlock_irq(&adapter->port_list_lock);
491 fc_remove_host(shost);
492 scsi_remove_host(shost);
493 scsi_host_put(shost);
494 adapter->scsi_host = NULL;
497 static struct fc_host_statistics*
498 zfcp_scsi_init_fc_host_stats(struct zfcp_adapter *adapter)
500 struct fc_host_statistics *fc_stats;
502 if (!adapter->fc_stats) {
503 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
504 if (!fc_stats)
505 return NULL;
506 adapter->fc_stats = fc_stats; /* freed in adapter_release */
508 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
509 return adapter->fc_stats;
512 static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
513 struct fsf_qtcb_bottom_port *data,
514 struct fsf_qtcb_bottom_port *old)
516 fc_stats->seconds_since_last_reset =
517 data->seconds_since_last_reset - old->seconds_since_last_reset;
518 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
519 fc_stats->tx_words = data->tx_words - old->tx_words;
520 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
521 fc_stats->rx_words = data->rx_words - old->rx_words;
522 fc_stats->lip_count = data->lip - old->lip;
523 fc_stats->nos_count = data->nos - old->nos;
524 fc_stats->error_frames = data->error_frames - old->error_frames;
525 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
526 fc_stats->link_failure_count = data->link_failure - old->link_failure;
527 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
528 fc_stats->loss_of_signal_count =
529 data->loss_of_signal - old->loss_of_signal;
530 fc_stats->prim_seq_protocol_err_count =
531 data->psp_error_counts - old->psp_error_counts;
532 fc_stats->invalid_tx_word_count =
533 data->invalid_tx_words - old->invalid_tx_words;
534 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
535 fc_stats->fcp_input_requests =
536 data->input_requests - old->input_requests;
537 fc_stats->fcp_output_requests =
538 data->output_requests - old->output_requests;
539 fc_stats->fcp_control_requests =
540 data->control_requests - old->control_requests;
541 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
542 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
545 static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics *fc_stats,
546 struct fsf_qtcb_bottom_port *data)
548 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
549 fc_stats->tx_frames = data->tx_frames;
550 fc_stats->tx_words = data->tx_words;
551 fc_stats->rx_frames = data->rx_frames;
552 fc_stats->rx_words = data->rx_words;
553 fc_stats->lip_count = data->lip;
554 fc_stats->nos_count = data->nos;
555 fc_stats->error_frames = data->error_frames;
556 fc_stats->dumped_frames = data->dumped_frames;
557 fc_stats->link_failure_count = data->link_failure;
558 fc_stats->loss_of_sync_count = data->loss_of_sync;
559 fc_stats->loss_of_signal_count = data->loss_of_signal;
560 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
561 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
562 fc_stats->invalid_crc_count = data->invalid_crcs;
563 fc_stats->fcp_input_requests = data->input_requests;
564 fc_stats->fcp_output_requests = data->output_requests;
565 fc_stats->fcp_control_requests = data->control_requests;
566 fc_stats->fcp_input_megabytes = data->input_mb;
567 fc_stats->fcp_output_megabytes = data->output_mb;
570 static struct fc_host_statistics *
571 zfcp_scsi_get_fc_host_stats(struct Scsi_Host *host)
573 struct zfcp_adapter *adapter;
574 struct fc_host_statistics *fc_stats;
575 struct fsf_qtcb_bottom_port *data;
576 int ret;
578 adapter = (struct zfcp_adapter *)host->hostdata[0];
579 fc_stats = zfcp_scsi_init_fc_host_stats(adapter);
580 if (!fc_stats)
581 return NULL;
583 data = kzalloc(sizeof(*data), GFP_KERNEL);
584 if (!data)
585 return NULL;
587 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
588 if (ret) {
589 kfree(data);
590 return NULL;
593 if (adapter->stats_reset &&
594 ((jiffies/HZ - adapter->stats_reset) <
595 data->seconds_since_last_reset))
596 zfcp_scsi_adjust_fc_host_stats(fc_stats, data,
597 adapter->stats_reset_data);
598 else
599 zfcp_scsi_set_fc_host_stats(fc_stats, data);
601 kfree(data);
602 return fc_stats;
605 static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host *shost)
607 struct zfcp_adapter *adapter;
608 struct fsf_qtcb_bottom_port *data;
609 int ret;
611 adapter = (struct zfcp_adapter *)shost->hostdata[0];
612 data = kzalloc(sizeof(*data), GFP_KERNEL);
613 if (!data)
614 return;
616 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
617 if (ret)
618 kfree(data);
619 else {
620 adapter->stats_reset = jiffies/HZ;
621 kfree(adapter->stats_reset_data);
622 adapter->stats_reset_data = data; /* finally freed in
623 adapter_release */
627 static void zfcp_scsi_get_host_port_state(struct Scsi_Host *shost)
629 struct zfcp_adapter *adapter =
630 (struct zfcp_adapter *)shost->hostdata[0];
631 int status = atomic_read(&adapter->status);
633 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
634 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
635 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
636 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
637 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
638 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
639 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
640 else
641 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
644 static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport *rport,
645 u32 timeout)
647 rport->dev_loss_tmo = timeout;
651 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
652 * @rport: The FC rport where to teminate I/O
654 * Abort all pending SCSI commands for a port by closing the
655 * port. Using a reopen avoids a conflict with a shutdown
656 * overwriting a reopen. The "forced" ensures that a disappeared port
657 * is not opened again as valid due to the cached plogi data in
658 * non-NPIV mode.
660 static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
662 struct zfcp_port *port;
663 struct Scsi_Host *shost = rport_to_shost(rport);
664 struct zfcp_adapter *adapter =
665 (struct zfcp_adapter *)shost->hostdata[0];
667 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
669 if (port) {
670 zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
671 put_device(&port->dev);
672 } else {
673 zfcp_erp_port_forced_no_port_dbf(
674 "sctrpin", adapter,
675 rport->port_name /* zfcp_scsi_rport_register */,
676 rport->port_id /* zfcp_scsi_rport_register */);
680 static void zfcp_scsi_rport_register(struct zfcp_port *port)
682 struct fc_rport_identifiers ids;
683 struct fc_rport *rport;
685 if (port->rport)
686 return;
688 ids.node_name = port->wwnn;
689 ids.port_name = port->wwpn;
690 ids.port_id = port->d_id;
691 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
693 zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
694 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
695 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
696 rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
697 if (!rport) {
698 dev_err(&port->adapter->ccw_device->dev,
699 "Registering port 0x%016Lx failed\n",
700 (unsigned long long)port->wwpn);
701 return;
704 rport->maxframe_size = port->maxframe_size;
705 rport->supported_classes = port->supported_classes;
706 port->rport = rport;
707 port->starget_id = rport->scsi_target_id;
709 zfcp_unit_queue_scsi_scan(port);
712 static void zfcp_scsi_rport_block(struct zfcp_port *port)
714 struct fc_rport *rport = port->rport;
716 if (rport) {
717 zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
718 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
719 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
720 fc_remote_port_delete(rport);
721 port->rport = NULL;
725 void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
727 get_device(&port->dev);
728 port->rport_task = RPORT_ADD;
730 if (!queue_work(port->adapter->work_queue, &port->rport_work))
731 put_device(&port->dev);
734 void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
736 get_device(&port->dev);
737 port->rport_task = RPORT_DEL;
739 if (port->rport && queue_work(port->adapter->work_queue,
740 &port->rport_work))
741 return;
743 put_device(&port->dev);
746 void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
748 unsigned long flags;
749 struct zfcp_port *port;
751 read_lock_irqsave(&adapter->port_list_lock, flags);
752 list_for_each_entry(port, &adapter->port_list, list)
753 zfcp_scsi_schedule_rport_block(port);
754 read_unlock_irqrestore(&adapter->port_list_lock, flags);
757 void zfcp_scsi_rport_work(struct work_struct *work)
759 struct zfcp_port *port = container_of(work, struct zfcp_port,
760 rport_work);
762 set_worker_desc("zrp%c-%16llx",
763 (port->rport_task == RPORT_ADD) ? 'a' : 'd',
764 port->wwpn); /* < WORKER_DESC_LEN=24 */
765 while (port->rport_task) {
766 if (port->rport_task == RPORT_ADD) {
767 port->rport_task = RPORT_NONE;
768 zfcp_scsi_rport_register(port);
769 } else {
770 port->rport_task = RPORT_NONE;
771 zfcp_scsi_rport_block(port);
775 put_device(&port->dev);
779 * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
780 * @adapter: The adapter where to configure DIF/DIX for the SCSI host
782 void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
784 unsigned int mask = 0;
785 unsigned int data_div;
786 struct Scsi_Host *shost = adapter->scsi_host;
788 data_div = atomic_read(&adapter->status) &
789 ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
791 if (enable_dif &&
792 adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
793 mask |= SHOST_DIF_TYPE1_PROTECTION;
795 if (enable_dif && data_div &&
796 adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
797 mask |= SHOST_DIX_TYPE1_PROTECTION;
798 scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
799 shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
800 shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
801 shost->max_sectors = shost->sg_tablesize * 8;
804 scsi_host_set_prot(shost, mask);
808 * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
809 * @scmd: The SCSI command to report the error for
810 * @ascq: The ASCQ to put in the sense buffer
812 * See the error handling in sd_done for the sense codes used here.
813 * Set DID_SOFT_ERROR to retry the request, if possible.
815 void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
817 scsi_build_sense_buffer(1, scmd->sense_buffer,
818 ILLEGAL_REQUEST, 0x10, ascq);
819 set_driver_byte(scmd, DRIVER_SENSE);
820 scmd->result |= SAM_STAT_CHECK_CONDITION;
821 set_host_byte(scmd, DID_SOFT_ERROR);
824 struct fc_function_template zfcp_transport_functions = {
825 .show_starget_port_id = 1,
826 .show_starget_port_name = 1,
827 .show_starget_node_name = 1,
828 .show_rport_supported_classes = 1,
829 .show_rport_maxframe_size = 1,
830 .show_rport_dev_loss_tmo = 1,
831 .show_host_node_name = 1,
832 .show_host_port_name = 1,
833 .show_host_permanent_port_name = 1,
834 .show_host_supported_classes = 1,
835 .show_host_supported_fc4s = 1,
836 .show_host_supported_speeds = 1,
837 .show_host_maxframe_size = 1,
838 .show_host_serial_number = 1,
839 .get_fc_host_stats = zfcp_scsi_get_fc_host_stats,
840 .reset_fc_host_stats = zfcp_scsi_reset_fc_host_stats,
841 .set_rport_dev_loss_tmo = zfcp_scsi_set_rport_dev_loss_tmo,
842 .get_host_port_state = zfcp_scsi_get_host_port_state,
843 .terminate_rport_io = zfcp_scsi_terminate_rport_io,
844 .show_host_port_state = 1,
845 .show_host_active_fc4s = 1,
846 .bsg_request = zfcp_fc_exec_bsg_job,
847 .bsg_timeout = zfcp_fc_timeout_bsg_job,
848 /* no functions registered for following dynamic attributes but
849 directly set by LLDD */
850 .show_host_port_type = 1,
851 .show_host_symbolic_name = 1,
852 .show_host_speed = 1,
853 .show_host_port_id = 1,
854 .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),