blob: d58bf79892f20c7240d7556abfba3f9ba6fc1960 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Swen Schillig41fa2ad2007-09-07 09:15:31 +02002/*
Christof Schmitt553448f2008-06-10 18:20:58 +02003 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Christof Schmitt553448f2008-06-10 18:20:58 +02005 * Interface to Linux SCSI midlayer.
Swen Schillig41fa2ad2007-09-07 09:15:31 +02006 *
Benjamin Blockbd168482020-05-08 19:23:29 +02007 * Copyright IBM Corp. 2002, 2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Christof Schmittecf39d42008-12-25 13:39:53 +010010#define KMSG_COMPONENT "zfcp"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020013#include <linux/module.h>
Christof Schmitt4318e082009-11-24 16:54:08 +010014#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Christof Schmitt4318e082009-11-24 16:54:08 +010016#include <scsi/fc/fc_fcp.h>
Felix Beckef3eb712010-07-16 15:37:42 +020017#include <scsi/scsi_eh.h>
Arun Sharma600634972011-07-26 16:09:06 -070018#include <linux/atomic.h>
Christof Schmittdcd20e22009-08-18 15:43:08 +020019#include "zfcp_ext.h"
20#include "zfcp_dbf.h"
Christof Schmitt7c7dc192009-11-24 16:54:13 +010021#include "zfcp_fc.h"
Christof Schmittb6bd2fb92010-02-17 11:18:50 +010022#include "zfcp_reqlist.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Christof Schmitta40a1ba2009-05-15 13:18:16 +020024static unsigned int default_depth = 32;
25module_param_named(queue_depth, default_depth, uint, 0600);
26MODULE_PARM_DESC(queue_depth, "Default queue depth for new SCSI devices");
27
Felix Beckef3eb712010-07-16 15:37:42 +020028static bool enable_dif;
Steffen Maiercc405ac2011-08-15 14:40:30 +020029module_param_named(dif, enable_dif, bool, 0400);
Fedor Loshakov636db602018-11-29 13:09:56 +010030MODULE_PARM_DESC(dif, "Enable DIF data integrity support (default off)");
31
32bool zfcp_experimental_dix;
33module_param_named(dix, zfcp_experimental_dix, bool, 0400);
34MODULE_PARM_DESC(dix, "Enable experimental DIX (data integrity extension) support which implies DIF support (default off)");
Felix Beckef3eb712010-07-16 15:37:42 +020035
Benjamin Block51569342017-07-28 12:30:50 +020036static bool allow_lun_scan = true;
Christof Schmitt0d81b4e2010-12-08 17:30:46 +010037module_param(allow_lun_scan, bool, 0600);
38MODULE_PARM_DESC(allow_lun_scan, "For NPIV, scan and attach all storage LUNs");
39
Christof Schmittb62a8d92010-09-08 14:39:55 +020040static void zfcp_scsi_slave_destroy(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Christof Schmittb62a8d92010-09-08 14:39:55 +020042 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
43
Steffen Maier44f747f2011-11-18 20:00:40 +010044 /* if previous slave_alloc returned early, there is nothing to do */
45 if (!zfcp_sdev->port)
46 return;
47
Christof Schmittb62a8d92010-09-08 14:39:55 +020048 zfcp_erp_lun_shutdown_wait(sdev, "scssd_1");
49 put_device(&zfcp_sdev->port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
Martin Petermannf76af7d72008-07-02 10:56:36 +020052static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 if (sdp->tagged_supported)
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +010055 scsi_change_queue_depth(sdp, default_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return 0;
57}
58
Martin Petermannf76af7d72008-07-02 10:56:36 +020059static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Martin Petermannfeac6a02008-07-02 10:56:35 +020061 set_host_byte(scpnt, result);
Swen Schillig250a1352010-12-02 15:16:15 +010062 zfcp_dbf_scsi_fail_send(scpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 scpnt->scsi_done(scpnt);
64}
65
Christof Schmitte55f8752010-11-18 14:53:18 +010066static
67int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Christof Schmittb62a8d92010-09-08 14:39:55 +020069 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +010070 struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
Christof Schmittb62a8d92010-09-08 14:39:55 +020071 int status, scsi_result, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /* reset the status for this request */
74 scpnt->result = 0;
75 scpnt->host_scribble = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Christof Schmitta2fa0ae2009-03-02 13:09:08 +010077 scsi_result = fc_remote_port_chkready(rport);
78 if (unlikely(scsi_result)) {
79 scpnt->result = scsi_result;
Swen Schillig250a1352010-12-02 15:16:15 +010080 zfcp_dbf_scsi_fail_send(scpnt);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +010081 scpnt->scsi_done(scpnt);
82 return 0;
83 }
84
Christof Schmittb62a8d92010-09-08 14:39:55 +020085 status = atomic_read(&zfcp_sdev->status);
Christof Schmitt88302712009-11-24 16:54:07 +010086 if (unlikely(status & ZFCP_STATUS_COMMON_ERP_FAILED) &&
Christof Schmittb62a8d92010-09-08 14:39:55 +020087 !(atomic_read(&zfcp_sdev->port->status) &
Christof Schmitt88302712009-11-24 16:54:07 +010088 ZFCP_STATUS_COMMON_ERP_FAILED)) {
Christof Schmittb62a8d92010-09-08 14:39:55 +020089 /* only LUN access denied, but port is good
Christof Schmitt88302712009-11-24 16:54:07 +010090 * not covered by FC transport, have to fail here */
Martin Petermannf76af7d72008-07-02 10:56:36 +020091 zfcp_scsi_command_fail(scpnt, DID_ERROR);
Joe Perchesa419aef2009-08-18 11:18:35 -070092 return 0;
Martin Petermannf76af7d72008-07-02 10:56:36 +020093 }
94
Christof Schmitt88302712009-11-24 16:54:07 +010095 if (unlikely(!(status & ZFCP_STATUS_COMMON_UNBLOCKED))) {
Steffen Maier6f2ce1c2016-12-09 17:16:33 +010096 /* This could be
Christof Schmitt88302712009-11-24 16:54:07 +010097 * call to rport_delete pending: mimic retry from
98 * fc_remote_port_chkready until rport is BLOCKED
99 */
100 zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY);
101 return 0;
102 }
103
Christof Schmittb62a8d92010-09-08 14:39:55 +0200104 ret = zfcp_fsf_fcp_cmnd(scpnt);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200105 if (unlikely(ret == -EBUSY))
Swen Schilligf7a65e92008-11-27 11:44:07 +0100106 return SCSI_MLQUEUE_DEVICE_BUSY;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200107 else if (unlikely(ret < 0))
108 return SCSI_MLQUEUE_HOST_BUSY;
109
110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Christof Schmittb62a8d92010-09-08 14:39:55 +0200113static int zfcp_scsi_slave_alloc(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200115 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct zfcp_port *port;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200120 struct zfcp_unit *unit;
Christof Schmitt0d81b4e2010-12-08 17:30:46 +0100121 int npiv = adapter->connection_features & FSF_FEATURE_NPIV_MODE;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200122
Steffen Maierab31fd02017-10-13 15:40:07 +0200123 zfcp_sdev->erp_action.adapter = adapter;
124 zfcp_sdev->erp_action.sdev = sdev;
125
Christof Schmittb62a8d92010-09-08 14:39:55 +0200126 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
127 if (!port)
128 return -ENXIO;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200129
Steffen Maierab31fd02017-10-13 15:40:07 +0200130 zfcp_sdev->erp_action.port = port;
131
Steffen Maieref4021f2019-05-23 15:23:46 +0200132 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() */
137 return -ENXIO;
138 }
139 mutex_unlock(&zfcp_sysfs_port_units_mutex);
140
Christof Schmittb62a8d92010-09-08 14:39:55 +0200141 unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
142 if (unit)
143 put_device(&unit->dev);
Christof Schmittf8210e32010-09-08 14:39:56 +0200144
Christof Schmitt0d81b4e2010-12-08 17:30:46 +0100145 if (!unit && !(allow_lun_scan && npiv)) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200146 put_device(&port->dev);
147 return -ENXIO;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200148 }
Christof Schmittb62a8d92010-09-08 14:39:55 +0200149
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);
158
Swen Schilligedaed852010-09-08 14:40:01 +0200159 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100160 zfcp_erp_lun_reopen(sdev, 0, "scsla_1");
Christof Schmittb62a8d92010-09-08 14:39:55 +0200161 zfcp_erp_wait(port->adapter);
162
163 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100166static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Christof Schmitt63caf362009-03-02 13:09:00 +0100168 struct Scsi_Host *scsi_host = scpnt->device->host;
169 struct zfcp_adapter *adapter =
170 (struct zfcp_adapter *) scsi_host->hostdata[0];
Christof Schmitt63caf362009-03-02 13:09:00 +0100171 struct zfcp_fsf_req *old_req, *abrt_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned long flags;
Christof Schmitta11a52b2009-07-13 15:06:14 +0200173 unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
Christof Schmitta1dbfdd2010-03-24 16:50:31 +0100174 int retval = SUCCESS, ret;
Christof Schmitt63caf362009-03-02 13:09:00 +0100175 int retry = 3;
Christof Schmitta11a52b2009-07-13 15:06:14 +0200176 char *dbf_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200178 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 write_lock_irqsave(&adapter->abort_lock, flags);
180
Christof Schmittb6bd2fb92010-02-17 11:18:50 +0100181 old_req = zfcp_reqlist_find(adapter->req_list, old_reqid);
Christof Schmitt63caf362009-03-02 13:09:00 +0100182 if (!old_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 write_unlock_irqrestore(&adapter->abort_lock, flags);
Swen Schillig250a1352010-12-02 15:16:15 +0100184 zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL);
Christof Schmittc6936e72009-04-17 15:08:11 +0200185 return FAILED; /* completion could be in progress */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100187 old_req->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200189 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200191
Christof Schmitt63caf362009-03-02 13:09:00 +0100192 while (retry--) {
Christof Schmittb62a8d92010-09-08 14:39:55 +0200193 abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt);
Christof Schmitt63caf362009-03-02 13:09:00 +0100194 if (abrt_req)
195 break;
196
Steffen Maier81979ae2018-05-17 19:14:44 +0200197 zfcp_dbf_scsi_abort("abrt_wt", scpnt, NULL);
Christof Schmitt63caf362009-03-02 13:09:00 +0100198 zfcp_erp_wait(adapter);
Christof Schmitta1dbfdd2010-03-24 16:50:31 +0100199 ret = fc_block_scsi_eh(scpnt);
Swen Schillig250a1352010-12-02 15:16:15 +0100200 if (ret) {
201 zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL);
Christof Schmitta1dbfdd2010-03-24 16:50:31 +0100202 return ret;
Swen Schillig250a1352010-12-02 15:16:15 +0100203 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100204 if (!(atomic_read(&adapter->status) &
205 ZFCP_STATUS_COMMON_RUNNING)) {
Swen Schillig250a1352010-12-02 15:16:15 +0100206 zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL);
Christof Schmitt63caf362009-03-02 13:09:00 +0100207 return SUCCESS;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Swen Schillig250a1352010-12-02 15:16:15 +0100210 if (!abrt_req) {
211 zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL);
Christof Schmitt63caf362009-03-02 13:09:00 +0100212 return FAILED;
Swen Schillig250a1352010-12-02 15:16:15 +0100213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Swen Schillig058b8642009-08-18 15:43:14 +0200215 wait_for_completion(&abrt_req->completion);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200216
Christof Schmitt63caf362009-03-02 13:09:00 +0100217 if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
Swen Schillig250a1352010-12-02 15:16:15 +0100218 dbf_tag = "abrt_ok";
Christof Schmitt63caf362009-03-02 13:09:00 +0100219 else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
Swen Schillig250a1352010-12-02 15:16:15 +0100220 dbf_tag = "abrt_nn";
Christof Schmitt63caf362009-03-02 13:09:00 +0100221 else {
Swen Schillig250a1352010-12-02 15:16:15 +0100222 dbf_tag = "abrt_fa";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Swen Schillig250a1352010-12-02 15:16:15 +0100225 zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req);
Christof Schmitt63caf362009-03-02 13:09:00 +0100226 zfcp_fsf_req_free(abrt_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return retval;
228}
229
Benjamin Blockdac37e12016-12-09 17:16:31 +0100230struct zfcp_scsi_req_filter {
231 u8 tmf_scope;
232 u32 lun_handle;
233 u32 port_handle;
234};
235
236static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
237{
238 struct zfcp_scsi_req_filter *filter =
239 (struct zfcp_scsi_req_filter *)data;
240
241 /* already aborted - prevent side-effects - or not a SCSI command */
Steffen Maierf9eca022018-11-08 15:44:45 +0100242 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)
Benjamin Blockdac37e12016-12-09 17:16:31 +0100245 return;
246
247 /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
248 if (old_req->qtcb->header.port_handle != filter->port_handle)
249 return;
250
251 if (filter->tmf_scope == FCP_TMF_LUN_RESET &&
252 old_req->qtcb->header.lun_handle != filter->lun_handle)
253 return;
254
255 zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
256 old_req->data = NULL;
257}
258
259static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags)
260{
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,
265 };
266 unsigned long flags;
267
268 if (tm_flags == FCP_TMF_LUN_RESET) {
269 filter.tmf_scope = FCP_TMF_LUN_RESET;
270 filter.lun_handle = zsdev->lun_handle;
271 }
272
273 /*
274 * abort_lock secures against other processings - in the abort-function
275 * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
276 */
277 write_lock_irqsave(&adapter->abort_lock, flags);
278 zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd,
279 &filter);
280 write_unlock_irqrestore(&adapter->abort_lock, flags);
281}
282
Steffen Maier674595d2018-05-17 19:14:56 +0200283/**
Steffen Maierd39eda52018-05-17 19:14:58 +0200284 * zfcp_scsi_task_mgmt_function() - Send a task management function (sync).
Steffen Maier674595d2018-05-17 19:14:56 +0200285 * @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.
288 */
Steffen Maierd39eda52018-05-17 19:14:58 +0200289static int zfcp_scsi_task_mgmt_function(struct scsi_device *sdev, u8 tm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Steffen Maier82212112018-05-17 19:14:50 +0200291 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
Christof Schmittb62a8d92010-09-08 14:39:55 +0200292 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Steffen Maier42afc652018-05-17 19:14:55 +0200293 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
Swen Schillig564e1c82009-08-18 15:43:19 +0200294 struct zfcp_fsf_req *fsf_req = NULL;
Christof Schmitta1dbfdd2010-03-24 16:50:31 +0100295 int retval = SUCCESS, ret;
Christof Schmitt63caf362009-03-02 13:09:00 +0100296 int retry = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Christof Schmitt63caf362009-03-02 13:09:00 +0100298 while (retry--) {
Steffen Maier26f5fa92018-05-17 19:14:54 +0200299 fsf_req = zfcp_fsf_fcp_task_mgmt(sdev, tm_flags);
Christof Schmitt63caf362009-03-02 13:09:00 +0100300 if (fsf_req)
301 break;
302
Steffen Maier82212112018-05-17 19:14:50 +0200303 zfcp_dbf_scsi_devreset("wait", sdev, tm_flags, NULL);
Christof Schmitt63caf362009-03-02 13:09:00 +0100304 zfcp_erp_wait(adapter);
Steffen Maier42afc652018-05-17 19:14:55 +0200305 ret = fc_block_rport(rport);
Steffen Maier1a5d9992017-07-28 12:30:55 +0200306 if (ret) {
Steffen Maier82212112018-05-17 19:14:50 +0200307 zfcp_dbf_scsi_devreset("fiof", sdev, tm_flags, NULL);
Christof Schmitta1dbfdd2010-03-24 16:50:31 +0100308 return ret;
Steffen Maier1a5d9992017-07-28 12:30:55 +0200309 }
Christof Schmitta1dbfdd2010-03-24 16:50:31 +0100310
Christof Schmitt63caf362009-03-02 13:09:00 +0100311 if (!(atomic_read(&adapter->status) &
312 ZFCP_STATUS_COMMON_RUNNING)) {
Steffen Maier82212112018-05-17 19:14:50 +0200313 zfcp_dbf_scsi_devreset("nres", sdev, tm_flags, NULL);
Christof Schmitt63caf362009-03-02 13:09:00 +0100314 return SUCCESS;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Steffen Maier1a5d9992017-07-28 12:30:55 +0200317 if (!fsf_req) {
Steffen Maier82212112018-05-17 19:14:50 +0200318 zfcp_dbf_scsi_devreset("reqf", sdev, tm_flags, NULL);
Christof Schmitt63caf362009-03-02 13:09:00 +0100319 return FAILED;
Steffen Maier1a5d9992017-07-28 12:30:55 +0200320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Swen Schillig058b8642009-08-18 15:43:14 +0200322 wait_for_completion(&fsf_req->completion);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200323
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200324 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
Steffen Maier82212112018-05-17 19:14:50 +0200325 zfcp_dbf_scsi_devreset("fail", sdev, tm_flags, fsf_req);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200326 retval = FAILED;
Benjamin Blockdac37e12016-12-09 17:16:31 +0100327 } else {
Steffen Maier82212112018-05-17 19:14:50 +0200328 zfcp_dbf_scsi_devreset("okay", sdev, tm_flags, fsf_req);
Benjamin Blockdac37e12016-12-09 17:16:31 +0100329 zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags);
330 }
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200331
332 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return retval;
334}
335
Martin Petermannf76af7d72008-07-02 10:56:36 +0200336static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
337{
Steffen Maier674595d2018-05-17 19:14:56 +0200338 struct scsi_device *sdev = scpnt->device;
339
Steffen Maierd39eda52018-05-17 19:14:58 +0200340 return zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_LUN_RESET);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200341}
342
343static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
344{
Steffen Maier674595d2018-05-17 19:14:56 +0200345 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];
351 int ret;
352
353 shost_for_each_device(tmp_sdev, shost) {
354 if (tmp_sdev->id == starget->id) {
355 sdev = tmp_sdev;
356 break;
357 }
358 }
359 if (!sdev) {
360 ret = FAILED;
361 zfcp_dbf_scsi_eh("tr_nosd", adapter, starget->id, ret);
362 return ret;
363 }
364
Steffen Maierd39eda52018-05-17 19:14:58 +0200365 ret = zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_TGT_RESET);
Steffen Maier674595d2018-05-17 19:14:56 +0200366
367 /* release reference from above shost_for_each_device */
368 if (sdev)
369 scsi_device_put(tmp_sdev);
370
371 return ret;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200372}
373
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100374static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Christof Schmittb62a8d92010-09-08 14:39:55 +0200376 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
377 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
Steffen Maierdf307812018-05-17 19:14:43 +0200378 int ret = SUCCESS, fc_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Steffen Maier242ec142019-03-26 14:36:59 +0100380 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE)) {
381 zfcp_erp_port_forced_reopen_all(adapter, 0, "schrh_p");
382 zfcp_erp_wait(adapter);
383 }
Swen Schilligea4a3a62010-12-02 15:16:16 +0100384 zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
Andreas Herrmann81654282006-09-18 22:30:36 +0200385 zfcp_erp_wait(adapter);
Steffen Maierdf307812018-05-17 19:14:43 +0200386 fc_ret = fc_block_scsi_eh(scpnt);
387 if (fc_ret)
388 ret = fc_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Steffen Maierdf307812018-05-17 19:14:43 +0200390 zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Steffen Maier35e91112018-05-17 19:15:03 +0200394/**
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.
398 *
399 * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise.
400 *
401 * This is similar to zfcp_sysfs_adapter_failed_store().
402 */
403static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host *shost, int reset_type)
404{
405 struct zfcp_adapter *adapter =
406 (struct zfcp_adapter *)shost->hostdata[0];
407 int ret = 0;
408
409 if (reset_type != SCSI_ADAPTER_RESET) {
410 ret = -EOPNOTSUPP;
411 zfcp_dbf_scsi_eh("scshr_n", adapter, ~0, ret);
412 return ret;
413 }
414
415 zfcp_erp_adapter_reset_sync(adapter, "scshr_y");
416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Christof Schmitt1947c722011-02-22 19:54:46 +0100419struct scsi_transport_template *zfcp_scsi_transport_template;
420
421static struct scsi_host_template zfcp_scsi_host_template = {
422 .module = THIS_MODULE,
423 .name = "zfcp",
424 .queuecommand = zfcp_scsi_queuecommand,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100425 .eh_timed_out = fc_eh_timed_out,
Christof Schmitt1947c722011-02-22 19:54:46 +0100426 .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,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100433 .change_queue_depth = scsi_change_queue_depth,
Steffen Maier35e91112018-05-17 19:15:03 +0200434 .host_reset = zfcp_scsi_sysfs_host_reset,
Christof Schmitt1947c722011-02-22 19:54:46 +0100435 .proc_name = "zfcp",
436 .can_queue = 4096,
437 .this_id = -1,
Steffen Maier5fea4292013-04-26 17:33:45 +0200438 .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 */
Steffen Maierb6319562019-01-24 17:40:34 +0100444 /* report size limit per scatter-gather segment */
445 .max_segment_size = ZFCP_QDIO_SBALE_LEN,
Christof Schmitt1947c722011-02-22 19:54:46 +0100446 .dma_boundary = ZFCP_QDIO_SBALE_LEN - 1,
Christof Schmitt1947c722011-02-22 19:54:46 +0100447 .shost_attrs = zfcp_sysfs_shost_attrs,
448 .sdev_attrs = zfcp_sysfs_sdev_attrs,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100449 .track_queue_depth = 1,
Steffen Maierb24bf222018-05-17 19:15:02 +0200450 .supported_mode = MODE_INITIATOR,
Christof Schmitt1947c722011-02-22 19:54:46 +0100451};
452
453/**
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200454 * zfcp_scsi_adapter_register() - Allocate and register SCSI and FC host with
455 * SCSI midlayer
Christof Schmitt1947c722011-02-22 19:54:46 +0100456 * @adapter: The zfcp adapter to register with the SCSI midlayer
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200457 *
458 * Allocates the SCSI host object for the given adapter, sets basic properties
459 * (such as the transport template, QDIO limits, ...), and registers it with
460 * the midlayer.
461 *
462 * During registration with the midlayer the corresponding FC host object for
463 * the referenced transport class is also implicitely allocated.
464 *
465 * Upon success adapter->scsi_host is set, and upon failure it remains NULL. If
466 * adapter->scsi_host is already set, nothing is done.
467 *
468 * Return:
469 * * 0 - Allocation and registration was successful
470 * * -EEXIST - SCSI and FC host did already exist, nothing was done, nothing
471 * was changed
472 * * -EIO - Allocation or registration failed
Christof Schmitt1947c722011-02-22 19:54:46 +0100473 */
474int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Martin Petermannf76af7d72008-07-02 10:56:36 +0200476 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Michael Loehr9f287452007-05-09 11:01:24 +0200478 if (adapter->scsi_host)
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200479 return -EEXIST;
Michael Loehr9f287452007-05-09 11:01:24 +0200480
Martin Petermannf76af7d72008-07-02 10:56:36 +0200481 ccw_device_get_id(adapter->ccw_device, &dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* register adapter as SCSI host with mid layer of SCSI stack */
Christof Schmitt1947c722011-02-22 19:54:46 +0100483 adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 sizeof (struct zfcp_adapter *));
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200485 if (!adapter->scsi_host)
486 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* tell the SCSI stack some characteristics of this adapter */
Christof Schmittf8210e32010-09-08 14:39:56 +0200489 adapter->scsi_host->max_id = 511;
490 adapter->scsi_host->max_lun = 0xFFFFFFFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 adapter->scsi_host->max_channel = 0;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200492 adapter->scsi_host->unique_id = dev_id.devno;
Christof Schmitt4318e082009-11-24 16:54:08 +0100493 adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
Christof Schmitt1947c722011-02-22 19:54:46 +0100494 adapter->scsi_host->transportt = zfcp_scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200496 /* make all basic properties known at registration time */
497 zfcp_qdio_shost_update(adapter, adapter->qdio);
498 zfcp_scsi_set_prot(adapter);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
501
502 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
503 scsi_host_put(adapter->scsi_host);
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200504 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Martin Petermannf76af7d72008-07-02 10:56:36 +0200506
507 return 0;
Benjamin Blockd0dff2a2020-05-08 19:23:35 +0200508err_out:
509 adapter->scsi_host = NULL;
510 dev_err(&adapter->ccw_device->dev,
511 "Registering the FCP device with the SCSI stack failed\n");
512 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Christof Schmitt1947c722011-02-22 19:54:46 +0100515/**
516 * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
517 * @adapter: The zfcp adapter to unregister.
518 */
519void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200522 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 shost = adapter->scsi_host;
525 if (!shost)
526 return;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200527
Swen Schilligecf0c772009-11-24 16:53:58 +0100528 read_lock_irq(&adapter->port_list_lock);
529 list_for_each_entry(port, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100530 port->rport = NULL;
Swen Schilligecf0c772009-11-24 16:53:58 +0100531 read_unlock_irq(&adapter->port_list_lock);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200532
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700533 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 scsi_remove_host(shost);
535 scsi_host_put(shost);
536 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100539static struct fc_host_statistics*
Steffen Maierd39eda52018-05-17 19:14:58 +0200540zfcp_scsi_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100542 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100544 if (!adapter->fc_stats) {
545 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
546 if (!fc_stats)
547 return NULL;
Swen Schilligf3450c72009-11-24 16:53:59 +0100548 adapter->fc_stats = fc_stats; /* freed in adapter_release */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100549 }
550 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
551 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Steffen Maierd39eda52018-05-17 19:14:58 +0200554static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
555 struct fsf_qtcb_bottom_port *data,
556 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Martin Petermannf76af7d72008-07-02 10:56:36 +0200558 fc_stats->seconds_since_last_reset =
559 data->seconds_since_last_reset - old->seconds_since_last_reset;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100560 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
561 fc_stats->tx_words = data->tx_words - old->tx_words;
562 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
563 fc_stats->rx_words = data->rx_words - old->rx_words;
564 fc_stats->lip_count = data->lip - old->lip;
565 fc_stats->nos_count = data->nos - old->nos;
566 fc_stats->error_frames = data->error_frames - old->error_frames;
567 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
568 fc_stats->link_failure_count = data->link_failure - old->link_failure;
569 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200570 fc_stats->loss_of_signal_count =
571 data->loss_of_signal - old->loss_of_signal;
572 fc_stats->prim_seq_protocol_err_count =
573 data->psp_error_counts - old->psp_error_counts;
574 fc_stats->invalid_tx_word_count =
575 data->invalid_tx_words - old->invalid_tx_words;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100576 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200577 fc_stats->fcp_input_requests =
578 data->input_requests - old->input_requests;
579 fc_stats->fcp_output_requests =
580 data->output_requests - old->output_requests;
581 fc_stats->fcp_control_requests =
582 data->control_requests - old->control_requests;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100583 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
584 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Steffen Maierd39eda52018-05-17 19:14:58 +0200587static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics *fc_stats,
588 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100590 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
591 fc_stats->tx_frames = data->tx_frames;
592 fc_stats->tx_words = data->tx_words;
593 fc_stats->rx_frames = data->rx_frames;
594 fc_stats->rx_words = data->rx_words;
595 fc_stats->lip_count = data->lip;
596 fc_stats->nos_count = data->nos;
597 fc_stats->error_frames = data->error_frames;
598 fc_stats->dumped_frames = data->dumped_frames;
599 fc_stats->link_failure_count = data->link_failure;
600 fc_stats->loss_of_sync_count = data->loss_of_sync;
601 fc_stats->loss_of_signal_count = data->loss_of_signal;
602 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
603 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
604 fc_stats->invalid_crc_count = data->invalid_crcs;
605 fc_stats->fcp_input_requests = data->input_requests;
606 fc_stats->fcp_output_requests = data->output_requests;
607 fc_stats->fcp_control_requests = data->control_requests;
608 fc_stats->fcp_input_megabytes = data->input_mb;
609 fc_stats->fcp_output_megabytes = data->output_mb;
610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Steffen Maierd39eda52018-05-17 19:14:58 +0200612static struct fc_host_statistics *
613zfcp_scsi_get_fc_host_stats(struct Scsi_Host *host)
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100614{
615 struct zfcp_adapter *adapter;
616 struct fc_host_statistics *fc_stats;
617 struct fsf_qtcb_bottom_port *data;
618 int ret;
619
Martin Petermannf76af7d72008-07-02 10:56:36 +0200620 adapter = (struct zfcp_adapter *)host->hostdata[0];
Steffen Maierd39eda52018-05-17 19:14:58 +0200621 fc_stats = zfcp_scsi_init_fc_host_stats(adapter);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100622 if (!fc_stats)
623 return NULL;
624
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200625 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100626 if (!data)
627 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100628
Swen Schillig564e1c82009-08-18 15:43:19 +0200629 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
Benjamin Block92953c62019-10-25 18:12:43 +0200630 if (ret != 0 && ret != -EAGAIN) {
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100631 kfree(data);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200632 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100633 }
634
635 if (adapter->stats_reset &&
636 ((jiffies/HZ - adapter->stats_reset) <
Martin Petermannf76af7d72008-07-02 10:56:36 +0200637 data->seconds_since_last_reset))
Steffen Maierd39eda52018-05-17 19:14:58 +0200638 zfcp_scsi_adjust_fc_host_stats(fc_stats, data,
639 adapter->stats_reset_data);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200640 else
Steffen Maierd39eda52018-05-17 19:14:58 +0200641 zfcp_scsi_set_fc_host_stats(fc_stats, data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100642
643 kfree(data);
644 return fc_stats;
645}
646
Steffen Maierd39eda52018-05-17 19:14:58 +0200647static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host *shost)
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100648{
649 struct zfcp_adapter *adapter;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200650 struct fsf_qtcb_bottom_port *data;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100651 int ret;
652
653 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200654 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100655 if (!data)
656 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100657
Swen Schillig564e1c82009-08-18 15:43:19 +0200658 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
Benjamin Block92953c62019-10-25 18:12:43 +0200659 if (ret != 0 && ret != -EAGAIN)
Heiko Carstens83f6d6d2007-08-08 10:47:02 +0200660 kfree(data);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200661 else {
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100662 adapter->stats_reset = jiffies/HZ;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200663 kfree(adapter->stats_reset_data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100664 adapter->stats_reset_data = data; /* finally freed in
Swen Schilligf3450c72009-11-24 16:53:59 +0100665 adapter_release */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Steffen Maierd39eda52018-05-17 19:14:58 +0200669static void zfcp_scsi_get_host_port_state(struct Scsi_Host *shost)
Sven Schuetz85a82392008-06-10 18:20:59 +0200670{
671 struct zfcp_adapter *adapter =
672 (struct zfcp_adapter *)shost->hostdata[0];
673 int status = atomic_read(&adapter->status);
674
675 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
676 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
677 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
678 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
679 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
680 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
681 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
682 else
683 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
684}
685
Steffen Maierd39eda52018-05-17 19:14:58 +0200686static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport *rport,
687 u32 timeout)
Andreas Herrmann338151e2006-05-22 18:25:56 +0200688{
689 rport->dev_loss_tmo = timeout;
690}
691
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100692/**
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100693 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
694 * @rport: The FC rport where to teminate I/O
695 *
696 * Abort all pending SCSI commands for a port by closing the
Christof Schmitt835dc292010-07-08 09:53:08 +0200697 * port. Using a reopen avoids a conflict with a shutdown
698 * overwriting a reopen. The "forced" ensures that a disappeared port
699 * is not opened again as valid due to the cached plogi data in
700 * non-NPIV mode.
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100701 */
702static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
703{
Christof Schmitt70932932009-04-17 15:08:15 +0200704 struct zfcp_port *port;
Swen Schilligea945ff2009-08-18 15:43:24 +0200705 struct Scsi_Host *shost = rport_to_shost(rport);
706 struct zfcp_adapter *adapter =
707 (struct zfcp_adapter *)shost->hostdata[0];
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100708
Swen Schilligea945ff2009-08-18 15:43:24 +0200709 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt70932932009-04-17 15:08:15 +0200710
711 if (port) {
Swen Schilligea4a3a62010-12-02 15:16:16 +0100712 zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
Christof Schmitt615f59e2010-02-17 11:18:56 +0100713 put_device(&port->dev);
Steffen Maier96d92702018-05-17 19:14:46 +0200714 } else {
715 zfcp_erp_port_forced_no_port_dbf(
716 "sctrpin", adapter,
717 rport->port_name /* zfcp_scsi_rport_register */,
718 rport->port_id /* zfcp_scsi_rport_register */);
Christof Schmitt70932932009-04-17 15:08:15 +0200719 }
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100720}
721
722static void zfcp_scsi_rport_register(struct zfcp_port *port)
723{
724 struct fc_rport_identifiers ids;
725 struct fc_rport *rport;
726
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200727 if (port->rport)
728 return;
729
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100730 ids.node_name = port->wwnn;
731 ids.port_name = port->wwpn;
732 ids.port_id = port->d_id;
733 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
734
Jens Remusfa89adb2018-05-03 13:52:47 +0200735 zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
736 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
737 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100738 rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
739 if (!rport) {
740 dev_err(&port->adapter->ccw_device->dev,
741 "Registering port 0x%016Lx failed\n",
742 (unsigned long long)port->wwpn);
743 return;
744 }
745
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100746 rport->maxframe_size = port->maxframe_size;
747 rport->supported_classes = port->supported_classes;
748 port->rport = rport;
Christof Schmitt1bf3ff02010-07-16 15:37:35 +0200749 port->starget_id = rport->scsi_target_id;
Christof Schmitt5a7de552010-07-08 09:53:07 +0200750
Christof Schmitt1daa4eb2010-09-08 14:39:52 +0200751 zfcp_unit_queue_scsi_scan(port);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100752}
753
754static void zfcp_scsi_rport_block(struct zfcp_port *port)
755{
Christof Schmitt70932932009-04-17 15:08:15 +0200756 struct fc_rport *rport = port->rport;
757
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200758 if (rport) {
Jens Remusfa89adb2018-05-03 13:52:47 +0200759 zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
760 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
761 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
Christof Schmitt70932932009-04-17 15:08:15 +0200762 fc_remote_port_delete(rport);
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200763 port->rport = NULL;
764 }
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100765}
766
767void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
768{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100769 get_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100770 port->rport_task = RPORT_ADD;
771
Swen Schillig45446832009-08-18 15:43:17 +0200772 if (!queue_work(port->adapter->work_queue, &port->rport_work))
Christof Schmitt615f59e2010-02-17 11:18:56 +0100773 put_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100774}
775
776void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
777{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100778 get_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100779 port->rport_task = RPORT_DEL;
780
Swen Schillig45446832009-08-18 15:43:17 +0200781 if (port->rport && queue_work(port->adapter->work_queue,
782 &port->rport_work))
Swen Schilliga67417a2009-08-18 15:43:06 +0200783 return;
784
Christof Schmitt615f59e2010-02-17 11:18:56 +0100785 put_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100786}
787
788void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
789{
Swen Schilligecf0c772009-11-24 16:53:58 +0100790 unsigned long flags;
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100791 struct zfcp_port *port;
792
Swen Schilligecf0c772009-11-24 16:53:58 +0100793 read_lock_irqsave(&adapter->port_list_lock, flags);
794 list_for_each_entry(port, &adapter->port_list, list)
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100795 zfcp_scsi_schedule_rport_block(port);
Swen Schilligecf0c772009-11-24 16:53:58 +0100796 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100797}
798
799void zfcp_scsi_rport_work(struct work_struct *work)
800{
801 struct zfcp_port *port = container_of(work, struct zfcp_port,
802 rport_work);
803
Steffen Maier5c750d52018-05-17 19:14:57 +0200804 set_worker_desc("zrp%c-%16llx",
805 (port->rport_task == RPORT_ADD) ? 'a' : 'd',
806 port->wwpn); /* < WORKER_DESC_LEN=24 */
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100807 while (port->rport_task) {
808 if (port->rport_task == RPORT_ADD) {
809 port->rport_task = RPORT_NONE;
810 zfcp_scsi_rport_register(port);
811 } else {
812 port->rport_task = RPORT_NONE;
813 zfcp_scsi_rport_block(port);
814 }
815 }
816
Christof Schmitt615f59e2010-02-17 11:18:56 +0100817 put_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100818}
819
Christof Schmitt5a7de552010-07-08 09:53:07 +0200820/**
Felix Beckef3eb712010-07-16 15:37:42 +0200821 * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
822 * @adapter: The adapter where to configure DIF/DIX for the SCSI host
823 */
824void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
825{
826 unsigned int mask = 0;
827 unsigned int data_div;
828 struct Scsi_Host *shost = adapter->scsi_host;
829
830 data_div = atomic_read(&adapter->status) &
831 ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
832
Fedor Loshakov636db602018-11-29 13:09:56 +0100833 if ((enable_dif || zfcp_experimental_dix) &&
Felix Beckef3eb712010-07-16 15:37:42 +0200834 adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
835 mask |= SHOST_DIF_TYPE1_PROTECTION;
836
Fedor Loshakov636db602018-11-29 13:09:56 +0100837 if (zfcp_experimental_dix && data_div &&
Felix Beckef3eb712010-07-16 15:37:42 +0200838 adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
839 mask |= SHOST_DIX_TYPE1_PROTECTION;
840 scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
Swen Schillig86a96682011-08-15 14:40:32 +0200841 shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
842 shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
843 shost->max_sectors = shost->sg_tablesize * 8;
Felix Beckef3eb712010-07-16 15:37:42 +0200844 }
845
846 scsi_host_set_prot(shost, mask);
847}
848
849/**
850 * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
851 * @scmd: The SCSI command to report the error for
852 * @ascq: The ASCQ to put in the sense buffer
853 *
854 * See the error handling in sd_done for the sense codes used here.
855 * Set DID_SOFT_ERROR to retry the request, if possible.
856 */
857void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
858{
859 scsi_build_sense_buffer(1, scmd->sense_buffer,
860 ILLEGAL_REQUEST, 0x10, ascq);
861 set_driver_byte(scmd, DRIVER_SENSE);
862 scmd->result |= SAM_STAT_CHECK_CONDITION;
863 set_host_byte(scmd, DID_SOFT_ERROR);
864}
865
Benjamin Blockbd168482020-05-08 19:23:29 +0200866void zfcp_scsi_shost_update_config_data(
867 struct zfcp_adapter *const adapter,
868 const struct fsf_qtcb_bottom_config *const bottom,
869 const bool bottom_incomplete)
870{
871 struct Scsi_Host *const shost = adapter->scsi_host;
872 const struct fc_els_flogi *nsp, *plogi;
873
874 if (shost == NULL)
875 return;
876
877 snprintf(fc_host_firmware_version(shost), FC_VERSION_STRING_SIZE,
878 "0x%08x", bottom->lic_version);
879
880 if (adapter->adapter_features & FSF_FEATURE_HBAAPI_MANAGEMENT) {
881 snprintf(fc_host_hardware_version(shost),
882 FC_VERSION_STRING_SIZE,
883 "0x%08x", bottom->hardware_version);
884 memcpy(fc_host_serial_number(shost), bottom->serial_number,
885 min(FC_SERIAL_NUMBER_SIZE, 17));
886 EBCASC(fc_host_serial_number(shost),
887 min(FC_SERIAL_NUMBER_SIZE, 17));
888 }
889
890 /* adjust pointers for missing command code */
891 nsp = (struct fc_els_flogi *) ((u8 *)&bottom->nport_serv_param
892 - sizeof(u32));
893 plogi = (struct fc_els_flogi *) ((u8 *)&bottom->plogi_payload
894 - sizeof(u32));
895
896 snprintf(fc_host_manufacturer(shost), FC_SERIAL_NUMBER_SIZE, "%s",
897 "IBM");
898 fc_host_port_name(shost) = be64_to_cpu(nsp->fl_wwpn);
899 fc_host_node_name(shost) = be64_to_cpu(nsp->fl_wwnn);
900 fc_host_supported_classes(shost) = FC_COS_CLASS2 | FC_COS_CLASS3;
901
902 zfcp_scsi_set_prot(adapter);
903
904 /* do not evaluate invalid fields */
905 if (bottom_incomplete)
906 return;
907
908 fc_host_port_id(shost) = ntoh24(bottom->s_id);
909 fc_host_speed(shost) =
910 zfcp_fsf_convert_portspeed(bottom->fc_link_speed);
911
912 snprintf(fc_host_model(shost), FC_SYMBOLIC_NAME_SIZE, "0x%04x",
913 bottom->adapter_type);
914
915 switch (bottom->fc_topology) {
916 case FSF_TOPO_P2P:
917 fc_host_port_type(shost) = FC_PORTTYPE_PTP;
918 fc_host_fabric_name(shost) = 0;
919 break;
920 case FSF_TOPO_FABRIC:
921 fc_host_fabric_name(shost) = be64_to_cpu(plogi->fl_wwnn);
922 if (bottom->connection_features & FSF_FEATURE_NPIV_MODE)
923 fc_host_port_type(shost) = FC_PORTTYPE_NPIV;
924 else
925 fc_host_port_type(shost) = FC_PORTTYPE_NPORT;
926 break;
927 case FSF_TOPO_AL:
928 fc_host_port_type(shost) = FC_PORTTYPE_NLPORT;
929 fallthrough;
930 default:
931 fc_host_fabric_name(shost) = 0;
932 break;
933 }
934}
935
Benjamin Block52e61fd2020-05-08 19:23:30 +0200936void zfcp_scsi_shost_update_port_data(
937 struct zfcp_adapter *const adapter,
938 const struct fsf_qtcb_bottom_port *const bottom)
939{
940 struct Scsi_Host *const shost = adapter->scsi_host;
941
942 if (shost == NULL)
943 return;
944
945 fc_host_permanent_port_name(shost) = bottom->wwpn;
946 fc_host_maxframe_size(shost) = bottom->maximum_frame_size;
947 fc_host_supported_speeds(shost) =
948 zfcp_fsf_convert_portspeed(bottom->supported_speed);
949 memcpy(fc_host_supported_fc4s(shost), bottom->supported_fc4_types,
950 FC_FC4_LIST_SIZE);
951 memcpy(fc_host_active_fc4s(shost), bottom->active_fc4_types,
952 FC_FC4_LIST_SIZE);
953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 .show_starget_port_id = 1,
957 .show_starget_port_name = 1,
958 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700959 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200960 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200961 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700962 .show_host_node_name = 1,
963 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100964 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700965 .show_host_supported_classes = 1,
Christof Schmitt0fdd2132009-11-24 16:54:17 +0100966 .show_host_supported_fc4s = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100967 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200968 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700969 .show_host_serial_number = 1,
Steffen Maier538c6e92020-03-12 18:44:58 +0100970 .show_host_manufacturer = 1,
971 .show_host_model = 1,
972 .show_host_hardware_version = 1,
973 .show_host_firmware_version = 1,
Steffen Maierd39eda52018-05-17 19:14:58 +0200974 .get_fc_host_stats = zfcp_scsi_get_fc_host_stats,
975 .reset_fc_host_stats = zfcp_scsi_reset_fc_host_stats,
976 .set_rport_dev_loss_tmo = zfcp_scsi_set_rport_dev_loss_tmo,
977 .get_host_port_state = zfcp_scsi_get_host_port_state,
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100978 .terminate_rport_io = zfcp_scsi_terminate_rport_io,
Sven Schuetz85a82392008-06-10 18:20:59 +0200979 .show_host_port_state = 1,
Christof Schmitt0fdd2132009-11-24 16:54:17 +0100980 .show_host_active_fc4s = 1,
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100981 .bsg_request = zfcp_fc_exec_bsg_job,
Swen Schillig491ca442010-01-14 17:19:01 +0100982 .bsg_timeout = zfcp_fc_timeout_bsg_job,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100983 /* no functions registered for following dynamic attributes but
984 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100985 .show_host_port_type = 1,
Christof Schmitt038d9442011-02-22 19:54:48 +0100986 .show_host_symbolic_name = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200987 .show_host_speed = 1,
988 .show_host_port_id = 1,
Steffen Maiere05a10a2020-03-12 18:44:57 +0100989 .show_host_fabric_name = 1,
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100990 .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991};