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