1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * zfcp device driver
4 *
5 * Interface to Linux SCSI midlayer.
6 *
7 * Copyright IBM Corp. 2002, 2018
8 */
9
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
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"
23
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
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");
31
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");
35
zfcp_scsi_slave_destroy(struct scsi_device * sdev)36 static void zfcp_scsi_slave_destroy(struct scsi_device *sdev)
37 {
38 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
39
40 /* if previous slave_alloc returned early, there is nothing to do */
41 if (!zfcp_sdev->port)
42 return;
43
44 zfcp_erp_lun_shutdown_wait(sdev, "scssd_1");
45 put_device(&zfcp_sdev->port->dev);
46 }
47
zfcp_scsi_slave_configure(struct scsi_device * sdp)48 static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
49 {
50 if (sdp->tagged_supported)
51 scsi_change_queue_depth(sdp, default_depth);
52 return 0;
53 }
54
zfcp_scsi_command_fail(struct scsi_cmnd * scpnt,int result)55 static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
56 {
57 set_host_byte(scpnt, result);
58 zfcp_dbf_scsi_fail_send(scpnt);
59 scpnt->scsi_done(scpnt);
60 }
61
62 static
zfcp_scsi_queuecommand(struct Scsi_Host * shost,struct scsi_cmnd * scpnt)63 int zfcp_scsi_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scpnt)
64 {
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;
68
69 /* reset the status for this request */
70 scpnt->result = 0;
71 scpnt->host_scribble = NULL;
72
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;
79 }
80
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;
89 }
90
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
95 */
96 zfcp_scsi_command_fail(scpnt, DID_IMM_RETRY);
97 return 0;
98 }
99
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;
105
106 return ret;
107 }
108
zfcp_scsi_slave_alloc(struct scsi_device * sdev)109 static int zfcp_scsi_slave_alloc(struct scsi_device *sdev)
110 {
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;
118
119 zfcp_sdev->erp_action.adapter = adapter;
120 zfcp_sdev->erp_action.sdev = sdev;
121
122 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
123 if (!port)
124 return -ENXIO;
125
126 zfcp_sdev->erp_action.port = port;
127
128 mutex_lock(&zfcp_sysfs_port_units_mutex);
129 if (zfcp_sysfs_port_is_removing(port)) {
130 /* port is already gone */
131 mutex_unlock(&zfcp_sysfs_port_units_mutex);
132 put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
133 return -ENXIO;
134 }
135 mutex_unlock(&zfcp_sysfs_port_units_mutex);
136
137 unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
138 if (unit)
139 put_device(&unit->dev);
140
141 if (!unit && !(allow_lun_scan && npiv)) {
142 put_device(&port->dev);
143 return -ENXIO;
144 }
145
146 zfcp_sdev->port = port;
147 zfcp_sdev->latencies.write.channel.min = 0xFFFFFFFF;
148 zfcp_sdev->latencies.write.fabric.min = 0xFFFFFFFF;
149 zfcp_sdev->latencies.read.channel.min = 0xFFFFFFFF;
150 zfcp_sdev->latencies.read.fabric.min = 0xFFFFFFFF;
151 zfcp_sdev->latencies.cmd.channel.min = 0xFFFFFFFF;
152 zfcp_sdev->latencies.cmd.fabric.min = 0xFFFFFFFF;
153 spin_lock_init(&zfcp_sdev->latencies.lock);
154
155 zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
156 zfcp_erp_lun_reopen(sdev, 0, "scsla_1");
157 zfcp_erp_wait(port->adapter);
158
159 return 0;
160 }
161
zfcp_scsi_eh_abort_handler(struct scsi_cmnd * scpnt)162 static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
163 {
164 struct Scsi_Host *scsi_host = scpnt->device->host;
165 struct zfcp_adapter *adapter =
166 (struct zfcp_adapter *) scsi_host->hostdata[0];
167 struct zfcp_fsf_req *old_req, *abrt_req;
168 unsigned long flags;
169 unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
170 int retval = SUCCESS, ret;
171 int retry = 3;
172 char *dbf_tag;
173
174 /* avoid race condition between late normal completion and abort */
175 write_lock_irqsave(&adapter->abort_lock, flags);
176
177 old_req = zfcp_reqlist_find(adapter->req_list, old_reqid);
178 if (!old_req) {
179 write_unlock_irqrestore(&adapter->abort_lock, flags);
180 zfcp_dbf_scsi_abort("abrt_or", scpnt, NULL);
181 return FAILED; /* completion could be in progress */
182 }
183 old_req->data = NULL;
184
185 /* don't access old fsf_req after releasing the abort_lock */
186 write_unlock_irqrestore(&adapter->abort_lock, flags);
187
188 while (retry--) {
189 abrt_req = zfcp_fsf_abort_fcp_cmnd(scpnt);
190 if (abrt_req)
191 break;
192
193 zfcp_dbf_scsi_abort("abrt_wt", scpnt, NULL);
194 zfcp_erp_wait(adapter);
195 ret = fc_block_scsi_eh(scpnt);
196 if (ret) {
197 zfcp_dbf_scsi_abort("abrt_bl", scpnt, NULL);
198 return ret;
199 }
200 if (!(atomic_read(&adapter->status) &
201 ZFCP_STATUS_COMMON_RUNNING)) {
202 zfcp_dbf_scsi_abort("abrt_ru", scpnt, NULL);
203 return SUCCESS;
204 }
205 }
206 if (!abrt_req) {
207 zfcp_dbf_scsi_abort("abrt_ar", scpnt, NULL);
208 return FAILED;
209 }
210
211 wait_for_completion(&abrt_req->completion);
212
213 if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
214 dbf_tag = "abrt_ok";
215 else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
216 dbf_tag = "abrt_nn";
217 else {
218 dbf_tag = "abrt_fa";
219 retval = FAILED;
220 }
221 zfcp_dbf_scsi_abort(dbf_tag, scpnt, abrt_req);
222 zfcp_fsf_req_free(abrt_req);
223 return retval;
224 }
225
226 struct zfcp_scsi_req_filter {
227 u8 tmf_scope;
228 u32 lun_handle;
229 u32 port_handle;
230 };
231
zfcp_scsi_forget_cmnd(struct zfcp_fsf_req * old_req,void * data)232 static void zfcp_scsi_forget_cmnd(struct zfcp_fsf_req *old_req, void *data)
233 {
234 struct zfcp_scsi_req_filter *filter =
235 (struct zfcp_scsi_req_filter *)data;
236
237 /* already aborted - prevent side-effects - or not a SCSI command */
238 if (old_req->data == NULL || old_req->fsf_command != FSF_QTCB_FCP_CMND)
239 return;
240
241 /* (tmf_scope == FCP_TMF_TGT_RESET || tmf_scope == FCP_TMF_LUN_RESET) */
242 if (old_req->qtcb->header.port_handle != filter->port_handle)
243 return;
244
245 if (filter->tmf_scope == FCP_TMF_LUN_RESET &&
246 old_req->qtcb->header.lun_handle != filter->lun_handle)
247 return;
248
249 zfcp_dbf_scsi_nullcmnd((struct scsi_cmnd *)old_req->data, old_req);
250 old_req->data = NULL;
251 }
252
zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev * zsdev,u8 tm_flags)253 static void zfcp_scsi_forget_cmnds(struct zfcp_scsi_dev *zsdev, u8 tm_flags)
254 {
255 struct zfcp_adapter *adapter = zsdev->port->adapter;
256 struct zfcp_scsi_req_filter filter = {
257 .tmf_scope = FCP_TMF_TGT_RESET,
258 .port_handle = zsdev->port->handle,
259 };
260 unsigned long flags;
261
262 if (tm_flags == FCP_TMF_LUN_RESET) {
263 filter.tmf_scope = FCP_TMF_LUN_RESET;
264 filter.lun_handle = zsdev->lun_handle;
265 }
266
267 /*
268 * abort_lock secures against other processings - in the abort-function
269 * and normal cmnd-handler - of (struct zfcp_fsf_req *)->data
270 */
271 write_lock_irqsave(&adapter->abort_lock, flags);
272 zfcp_reqlist_apply_for_all(adapter->req_list, zfcp_scsi_forget_cmnd,
273 &filter);
274 write_unlock_irqrestore(&adapter->abort_lock, flags);
275 }
276
277 /**
278 * zfcp_scsi_task_mgmt_function() - Send a task management function (sync).
279 * @sdev: Pointer to SCSI device to send the task management command to.
280 * @tm_flags: Task management flags,
281 * here we only handle %FCP_TMF_TGT_RESET or %FCP_TMF_LUN_RESET.
282 */
zfcp_scsi_task_mgmt_function(struct scsi_device * sdev,u8 tm_flags)283 static int zfcp_scsi_task_mgmt_function(struct scsi_device *sdev, u8 tm_flags)
284 {
285 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);
286 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
287 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
288 struct zfcp_fsf_req *fsf_req = NULL;
289 int retval = SUCCESS, ret;
290 int retry = 3;
291
292 while (retry--) {
293 fsf_req = zfcp_fsf_fcp_task_mgmt(sdev, tm_flags);
294 if (fsf_req)
295 break;
296
297 zfcp_dbf_scsi_devreset("wait", sdev, tm_flags, NULL);
298 zfcp_erp_wait(adapter);
299 ret = fc_block_rport(rport);
300 if (ret) {
301 zfcp_dbf_scsi_devreset("fiof", sdev, tm_flags, NULL);
302 return ret;
303 }
304
305 if (!(atomic_read(&adapter->status) &
306 ZFCP_STATUS_COMMON_RUNNING)) {
307 zfcp_dbf_scsi_devreset("nres", sdev, tm_flags, NULL);
308 return SUCCESS;
309 }
310 }
311 if (!fsf_req) {
312 zfcp_dbf_scsi_devreset("reqf", sdev, tm_flags, NULL);
313 return FAILED;
314 }
315
316 wait_for_completion(&fsf_req->completion);
317
318 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
319 zfcp_dbf_scsi_devreset("fail", sdev, tm_flags, fsf_req);
320 retval = FAILED;
321 } else {
322 zfcp_dbf_scsi_devreset("okay", sdev, tm_flags, fsf_req);
323 zfcp_scsi_forget_cmnds(zfcp_sdev, tm_flags);
324 }
325
326 zfcp_fsf_req_free(fsf_req);
327 return retval;
328 }
329
zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd * scpnt)330 static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
331 {
332 struct scsi_device *sdev = scpnt->device;
333
334 return zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_LUN_RESET);
335 }
336
zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd * scpnt)337 static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
338 {
339 struct scsi_target *starget = scsi_target(scpnt->device);
340 struct fc_rport *rport = starget_to_rport(starget);
341 struct Scsi_Host *shost = rport_to_shost(rport);
342 struct scsi_device *sdev = NULL, *tmp_sdev;
343 struct zfcp_adapter *adapter =
344 (struct zfcp_adapter *)shost->hostdata[0];
345 int ret;
346
347 shost_for_each_device(tmp_sdev, shost) {
348 if (tmp_sdev->id == starget->id) {
349 sdev = tmp_sdev;
350 break;
351 }
352 }
353 if (!sdev) {
354 ret = FAILED;
355 zfcp_dbf_scsi_eh("tr_nosd", adapter, starget->id, ret);
356 return ret;
357 }
358
359 ret = zfcp_scsi_task_mgmt_function(sdev, FCP_TMF_TGT_RESET);
360
361 /* release reference from above shost_for_each_device */
362 if (sdev)
363 scsi_device_put(tmp_sdev);
364
365 return ret;
366 }
367
zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd * scpnt)368 static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
369 {
370 struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(scpnt->device);
371 struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;
372 int ret = SUCCESS, fc_ret;
373
374 if (!(adapter->connection_features & FSF_FEATURE_NPIV_MODE)) {
375 zfcp_erp_port_forced_reopen_all(adapter, 0, "schrh_p");
376 zfcp_erp_wait(adapter);
377 }
378 zfcp_erp_adapter_reopen(adapter, 0, "schrh_1");
379 zfcp_erp_wait(adapter);
380 fc_ret = fc_block_scsi_eh(scpnt);
381 if (fc_ret)
382 ret = fc_ret;
383
384 zfcp_dbf_scsi_eh("schrh_r", adapter, ~0, ret);
385 return ret;
386 }
387
388 /**
389 * zfcp_scsi_sysfs_host_reset() - Support scsi_host sysfs attribute host_reset.
390 * @shost: Pointer to Scsi_Host to perform action on.
391 * @reset_type: We support %SCSI_ADAPTER_RESET but not %SCSI_FIRMWARE_RESET.
392 *
393 * Return: 0 on %SCSI_ADAPTER_RESET, -%EOPNOTSUPP otherwise.
394 *
395 * This is similar to zfcp_sysfs_adapter_failed_store().
396 */
zfcp_scsi_sysfs_host_reset(struct Scsi_Host * shost,int reset_type)397 static int zfcp_scsi_sysfs_host_reset(struct Scsi_Host *shost, int reset_type)
398 {
399 struct zfcp_adapter *adapter =
400 (struct zfcp_adapter *)shost->hostdata[0];
401 int ret = 0;
402
403 if (reset_type != SCSI_ADAPTER_RESET) {
404 ret = -EOPNOTSUPP;
405 zfcp_dbf_scsi_eh("scshr_n", adapter, ~0, ret);
406 return ret;
407 }
408
409 zfcp_erp_adapter_reset_sync(adapter, "scshr_y");
410 return ret;
411 }
412
413 struct scsi_transport_template *zfcp_scsi_transport_template;
414
415 static struct scsi_host_template zfcp_scsi_host_template = {
416 .module = THIS_MODULE,
417 .name = "zfcp",
418 .queuecommand = zfcp_scsi_queuecommand,
419 .eh_timed_out = fc_eh_timed_out,
420 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
421 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
422 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
423 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
424 .slave_alloc = zfcp_scsi_slave_alloc,
425 .slave_configure = zfcp_scsi_slave_configure,
426 .slave_destroy = zfcp_scsi_slave_destroy,
427 .change_queue_depth = scsi_change_queue_depth,
428 .host_reset = zfcp_scsi_sysfs_host_reset,
429 .proc_name = "zfcp",
430 .can_queue = 4096,
431 .this_id = -1,
432 .sg_tablesize = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
433 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2),
434 /* GCD, adjusted later */
435 .max_sectors = (((QDIO_MAX_ELEMENTS_PER_BUFFER - 1)
436 * ZFCP_QDIO_MAX_SBALS_PER_REQ) - 2) * 8,
437 /* GCD, adjusted later */
438 .dma_boundary = ZFCP_QDIO_SBALE_LEN - 1,
439 .use_clustering = 1,
440 .shost_attrs = zfcp_sysfs_shost_attrs,
441 .sdev_attrs = zfcp_sysfs_sdev_attrs,
442 .track_queue_depth = 1,
443 .supported_mode = MODE_INITIATOR,
444 };
445
446 /**
447 * zfcp_scsi_adapter_register - Register SCSI and FC host with SCSI midlayer
448 * @adapter: The zfcp adapter to register with the SCSI midlayer
449 */
zfcp_scsi_adapter_register(struct zfcp_adapter * adapter)450 int zfcp_scsi_adapter_register(struct zfcp_adapter *adapter)
451 {
452 struct ccw_dev_id dev_id;
453
454 if (adapter->scsi_host)
455 return 0;
456
457 ccw_device_get_id(adapter->ccw_device, &dev_id);
458 /* register adapter as SCSI host with mid layer of SCSI stack */
459 adapter->scsi_host = scsi_host_alloc(&zfcp_scsi_host_template,
460 sizeof (struct zfcp_adapter *));
461 if (!adapter->scsi_host) {
462 dev_err(&adapter->ccw_device->dev,
463 "Registering the FCP device with the "
464 "SCSI stack failed\n");
465 return -EIO;
466 }
467
468 /* tell the SCSI stack some characteristics of this adapter */
469 adapter->scsi_host->max_id = 511;
470 adapter->scsi_host->max_lun = 0xFFFFFFFF;
471 adapter->scsi_host->max_channel = 0;
472 adapter->scsi_host->unique_id = dev_id.devno;
473 adapter->scsi_host->max_cmd_len = 16; /* in struct fcp_cmnd */
474 adapter->scsi_host->transportt = zfcp_scsi_transport_template;
475
476 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
477
478 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
479 scsi_host_put(adapter->scsi_host);
480 return -EIO;
481 }
482
483 return 0;
484 }
485
486 /**
487 * zfcp_scsi_adapter_unregister - Unregister SCSI and FC host from SCSI midlayer
488 * @adapter: The zfcp adapter to unregister.
489 */
zfcp_scsi_adapter_unregister(struct zfcp_adapter * adapter)490 void zfcp_scsi_adapter_unregister(struct zfcp_adapter *adapter)
491 {
492 struct Scsi_Host *shost;
493 struct zfcp_port *port;
494
495 shost = adapter->scsi_host;
496 if (!shost)
497 return;
498
499 read_lock_irq(&adapter->port_list_lock);
500 list_for_each_entry(port, &adapter->port_list, list)
501 port->rport = NULL;
502 read_unlock_irq(&adapter->port_list_lock);
503
504 fc_remove_host(shost);
505 scsi_remove_host(shost);
506 scsi_host_put(shost);
507 adapter->scsi_host = NULL;
508 }
509
510 static struct fc_host_statistics*
zfcp_scsi_init_fc_host_stats(struct zfcp_adapter * adapter)511 zfcp_scsi_init_fc_host_stats(struct zfcp_adapter *adapter)
512 {
513 struct fc_host_statistics *fc_stats;
514
515 if (!adapter->fc_stats) {
516 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
517 if (!fc_stats)
518 return NULL;
519 adapter->fc_stats = fc_stats; /* freed in adapter_release */
520 }
521 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
522 return adapter->fc_stats;
523 }
524
zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics * fc_stats,struct fsf_qtcb_bottom_port * data,struct fsf_qtcb_bottom_port * old)525 static void zfcp_scsi_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
526 struct fsf_qtcb_bottom_port *data,
527 struct fsf_qtcb_bottom_port *old)
528 {
529 fc_stats->seconds_since_last_reset =
530 data->seconds_since_last_reset - old->seconds_since_last_reset;
531 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
532 fc_stats->tx_words = data->tx_words - old->tx_words;
533 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
534 fc_stats->rx_words = data->rx_words - old->rx_words;
535 fc_stats->lip_count = data->lip - old->lip;
536 fc_stats->nos_count = data->nos - old->nos;
537 fc_stats->error_frames = data->error_frames - old->error_frames;
538 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
539 fc_stats->link_failure_count = data->link_failure - old->link_failure;
540 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
541 fc_stats->loss_of_signal_count =
542 data->loss_of_signal - old->loss_of_signal;
543 fc_stats->prim_seq_protocol_err_count =
544 data->psp_error_counts - old->psp_error_counts;
545 fc_stats->invalid_tx_word_count =
546 data->invalid_tx_words - old->invalid_tx_words;
547 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
548 fc_stats->fcp_input_requests =
549 data->input_requests - old->input_requests;
550 fc_stats->fcp_output_requests =
551 data->output_requests - old->output_requests;
552 fc_stats->fcp_control_requests =
553 data->control_requests - old->control_requests;
554 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
555 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
556 }
557
zfcp_scsi_set_fc_host_stats(struct fc_host_statistics * fc_stats,struct fsf_qtcb_bottom_port * data)558 static void zfcp_scsi_set_fc_host_stats(struct fc_host_statistics *fc_stats,
559 struct fsf_qtcb_bottom_port *data)
560 {
561 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
562 fc_stats->tx_frames = data->tx_frames;
563 fc_stats->tx_words = data->tx_words;
564 fc_stats->rx_frames = data->rx_frames;
565 fc_stats->rx_words = data->rx_words;
566 fc_stats->lip_count = data->lip;
567 fc_stats->nos_count = data->nos;
568 fc_stats->error_frames = data->error_frames;
569 fc_stats->dumped_frames = data->dumped_frames;
570 fc_stats->link_failure_count = data->link_failure;
571 fc_stats->loss_of_sync_count = data->loss_of_sync;
572 fc_stats->loss_of_signal_count = data->loss_of_signal;
573 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
574 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
575 fc_stats->invalid_crc_count = data->invalid_crcs;
576 fc_stats->fcp_input_requests = data->input_requests;
577 fc_stats->fcp_output_requests = data->output_requests;
578 fc_stats->fcp_control_requests = data->control_requests;
579 fc_stats->fcp_input_megabytes = data->input_mb;
580 fc_stats->fcp_output_megabytes = data->output_mb;
581 }
582
583 static struct fc_host_statistics *
zfcp_scsi_get_fc_host_stats(struct Scsi_Host * host)584 zfcp_scsi_get_fc_host_stats(struct Scsi_Host *host)
585 {
586 struct zfcp_adapter *adapter;
587 struct fc_host_statistics *fc_stats;
588 struct fsf_qtcb_bottom_port *data;
589 int ret;
590
591 adapter = (struct zfcp_adapter *)host->hostdata[0];
592 fc_stats = zfcp_scsi_init_fc_host_stats(adapter);
593 if (!fc_stats)
594 return NULL;
595
596 data = kzalloc(sizeof(*data), GFP_KERNEL);
597 if (!data)
598 return NULL;
599
600 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
601 if (ret) {
602 kfree(data);
603 return NULL;
604 }
605
606 if (adapter->stats_reset &&
607 ((jiffies/HZ - adapter->stats_reset) <
608 data->seconds_since_last_reset))
609 zfcp_scsi_adjust_fc_host_stats(fc_stats, data,
610 adapter->stats_reset_data);
611 else
612 zfcp_scsi_set_fc_host_stats(fc_stats, data);
613
614 kfree(data);
615 return fc_stats;
616 }
617
zfcp_scsi_reset_fc_host_stats(struct Scsi_Host * shost)618 static void zfcp_scsi_reset_fc_host_stats(struct Scsi_Host *shost)
619 {
620 struct zfcp_adapter *adapter;
621 struct fsf_qtcb_bottom_port *data;
622 int ret;
623
624 adapter = (struct zfcp_adapter *)shost->hostdata[0];
625 data = kzalloc(sizeof(*data), GFP_KERNEL);
626 if (!data)
627 return;
628
629 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
630 if (ret)
631 kfree(data);
632 else {
633 adapter->stats_reset = jiffies/HZ;
634 kfree(adapter->stats_reset_data);
635 adapter->stats_reset_data = data; /* finally freed in
636 adapter_release */
637 }
638 }
639
zfcp_scsi_get_host_port_state(struct Scsi_Host * shost)640 static void zfcp_scsi_get_host_port_state(struct Scsi_Host *shost)
641 {
642 struct zfcp_adapter *adapter =
643 (struct zfcp_adapter *)shost->hostdata[0];
644 int status = atomic_read(&adapter->status);
645
646 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
647 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
648 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
649 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
650 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
651 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
652 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
653 else
654 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
655 }
656
zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport * rport,u32 timeout)657 static void zfcp_scsi_set_rport_dev_loss_tmo(struct fc_rport *rport,
658 u32 timeout)
659 {
660 rport->dev_loss_tmo = timeout;
661 }
662
663 /**
664 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
665 * @rport: The FC rport where to teminate I/O
666 *
667 * Abort all pending SCSI commands for a port by closing the
668 * port. Using a reopen avoids a conflict with a shutdown
669 * overwriting a reopen. The "forced" ensures that a disappeared port
670 * is not opened again as valid due to the cached plogi data in
671 * non-NPIV mode.
672 */
zfcp_scsi_terminate_rport_io(struct fc_rport * rport)673 static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
674 {
675 struct zfcp_port *port;
676 struct Scsi_Host *shost = rport_to_shost(rport);
677 struct zfcp_adapter *adapter =
678 (struct zfcp_adapter *)shost->hostdata[0];
679
680 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
681
682 if (port) {
683 zfcp_erp_port_forced_reopen(port, 0, "sctrpi1");
684 put_device(&port->dev);
685 } else {
686 zfcp_erp_port_forced_no_port_dbf(
687 "sctrpin", adapter,
688 rport->port_name /* zfcp_scsi_rport_register */,
689 rport->port_id /* zfcp_scsi_rport_register */);
690 }
691 }
692
zfcp_scsi_rport_register(struct zfcp_port * port)693 static void zfcp_scsi_rport_register(struct zfcp_port *port)
694 {
695 struct fc_rport_identifiers ids;
696 struct fc_rport *rport;
697
698 if (port->rport)
699 return;
700
701 ids.node_name = port->wwnn;
702 ids.port_name = port->wwpn;
703 ids.port_id = port->d_id;
704 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
705
706 zfcp_dbf_rec_trig_lock("scpaddy", port->adapter, port, NULL,
707 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD,
708 ZFCP_PSEUDO_ERP_ACTION_RPORT_ADD);
709 rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
710 if (!rport) {
711 dev_err(&port->adapter->ccw_device->dev,
712 "Registering port 0x%016Lx failed\n",
713 (unsigned long long)port->wwpn);
714 return;
715 }
716
717 rport->maxframe_size = port->maxframe_size;
718 rport->supported_classes = port->supported_classes;
719 port->rport = rport;
720 port->starget_id = rport->scsi_target_id;
721
722 zfcp_unit_queue_scsi_scan(port);
723 }
724
zfcp_scsi_rport_block(struct zfcp_port * port)725 static void zfcp_scsi_rport_block(struct zfcp_port *port)
726 {
727 struct fc_rport *rport = port->rport;
728
729 if (rport) {
730 zfcp_dbf_rec_trig_lock("scpdely", port->adapter, port, NULL,
731 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL,
732 ZFCP_PSEUDO_ERP_ACTION_RPORT_DEL);
733 fc_remote_port_delete(rport);
734 port->rport = NULL;
735 }
736 }
737
zfcp_scsi_schedule_rport_register(struct zfcp_port * port)738 void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
739 {
740 get_device(&port->dev);
741 port->rport_task = RPORT_ADD;
742
743 if (!queue_work(port->adapter->work_queue, &port->rport_work))
744 put_device(&port->dev);
745 }
746
zfcp_scsi_schedule_rport_block(struct zfcp_port * port)747 void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
748 {
749 get_device(&port->dev);
750 port->rport_task = RPORT_DEL;
751
752 if (port->rport && queue_work(port->adapter->work_queue,
753 &port->rport_work))
754 return;
755
756 put_device(&port->dev);
757 }
758
zfcp_scsi_schedule_rports_block(struct zfcp_adapter * adapter)759 void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
760 {
761 unsigned long flags;
762 struct zfcp_port *port;
763
764 read_lock_irqsave(&adapter->port_list_lock, flags);
765 list_for_each_entry(port, &adapter->port_list, list)
766 zfcp_scsi_schedule_rport_block(port);
767 read_unlock_irqrestore(&adapter->port_list_lock, flags);
768 }
769
zfcp_scsi_rport_work(struct work_struct * work)770 void zfcp_scsi_rport_work(struct work_struct *work)
771 {
772 struct zfcp_port *port = container_of(work, struct zfcp_port,
773 rport_work);
774
775 set_worker_desc("zrp%c-%16llx",
776 (port->rport_task == RPORT_ADD) ? 'a' : 'd',
777 port->wwpn); /* < WORKER_DESC_LEN=24 */
778 while (port->rport_task) {
779 if (port->rport_task == RPORT_ADD) {
780 port->rport_task = RPORT_NONE;
781 zfcp_scsi_rport_register(port);
782 } else {
783 port->rport_task = RPORT_NONE;
784 zfcp_scsi_rport_block(port);
785 }
786 }
787
788 put_device(&port->dev);
789 }
790
791 /**
792 * zfcp_scsi_set_prot - Configure DIF/DIX support in scsi_host
793 * @adapter: The adapter where to configure DIF/DIX for the SCSI host
794 */
zfcp_scsi_set_prot(struct zfcp_adapter * adapter)795 void zfcp_scsi_set_prot(struct zfcp_adapter *adapter)
796 {
797 unsigned int mask = 0;
798 unsigned int data_div;
799 struct Scsi_Host *shost = adapter->scsi_host;
800
801 data_div = atomic_read(&adapter->status) &
802 ZFCP_STATUS_ADAPTER_DATA_DIV_ENABLED;
803
804 if (enable_dif &&
805 adapter->adapter_features & FSF_FEATURE_DIF_PROT_TYPE1)
806 mask |= SHOST_DIF_TYPE1_PROTECTION;
807
808 if (enable_dif && data_div &&
809 adapter->adapter_features & FSF_FEATURE_DIX_PROT_TCPIP) {
810 mask |= SHOST_DIX_TYPE1_PROTECTION;
811 scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
812 shost->sg_prot_tablesize = adapter->qdio->max_sbale_per_req / 2;
813 shost->sg_tablesize = adapter->qdio->max_sbale_per_req / 2;
814 shost->max_sectors = shost->sg_tablesize * 8;
815 }
816
817 scsi_host_set_prot(shost, mask);
818 }
819
820 /**
821 * zfcp_scsi_dif_sense_error - Report DIF/DIX error as driver sense error
822 * @scmd: The SCSI command to report the error for
823 * @ascq: The ASCQ to put in the sense buffer
824 *
825 * See the error handling in sd_done for the sense codes used here.
826 * Set DID_SOFT_ERROR to retry the request, if possible.
827 */
zfcp_scsi_dif_sense_error(struct scsi_cmnd * scmd,int ascq)828 void zfcp_scsi_dif_sense_error(struct scsi_cmnd *scmd, int ascq)
829 {
830 scsi_build_sense_buffer(1, scmd->sense_buffer,
831 ILLEGAL_REQUEST, 0x10, ascq);
832 set_driver_byte(scmd, DRIVER_SENSE);
833 scmd->result |= SAM_STAT_CHECK_CONDITION;
834 set_host_byte(scmd, DID_SOFT_ERROR);
835 }
836
837 struct fc_function_template zfcp_transport_functions = {
838 .show_starget_port_id = 1,
839 .show_starget_port_name = 1,
840 .show_starget_node_name = 1,
841 .show_rport_supported_classes = 1,
842 .show_rport_maxframe_size = 1,
843 .show_rport_dev_loss_tmo = 1,
844 .show_host_node_name = 1,
845 .show_host_port_name = 1,
846 .show_host_permanent_port_name = 1,
847 .show_host_supported_classes = 1,
848 .show_host_supported_fc4s = 1,
849 .show_host_supported_speeds = 1,
850 .show_host_maxframe_size = 1,
851 .show_host_serial_number = 1,
852 .get_fc_host_stats = zfcp_scsi_get_fc_host_stats,
853 .reset_fc_host_stats = zfcp_scsi_reset_fc_host_stats,
854 .set_rport_dev_loss_tmo = zfcp_scsi_set_rport_dev_loss_tmo,
855 .get_host_port_state = zfcp_scsi_get_host_port_state,
856 .terminate_rport_io = zfcp_scsi_terminate_rport_io,
857 .show_host_port_state = 1,
858 .show_host_active_fc4s = 1,
859 .bsg_request = zfcp_fc_exec_bsg_job,
860 .bsg_timeout = zfcp_fc_timeout_bsg_job,
861 /* no functions registered for following dynamic attributes but
862 directly set by LLDD */
863 .show_host_port_type = 1,
864 .show_host_symbolic_name = 1,
865 .show_host_speed = 1,
866 .show_host_port_id = 1,
867 .dd_bsg_size = sizeof(struct zfcp_fsf_ct_els),
868 };
869