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