blob: ad11547017294c3fc410fa0ef5abe585c584cd75 [file] [log] [blame]
Swen Schillig41fa2ad2007-09-07 09:15:31 +02001/*
Christof Schmitt553448f2008-06-10 18:20:58 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Christof Schmitt553448f2008-06-10 18:20:58 +02004 * Interface to Linux SCSI midlayer.
Swen Schillig41fa2ad2007-09-07 09:15:31 +02005 *
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01006 * Copyright IBM Corporation 2002, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Christof Schmitt5f852be2007-05-08 11:16:52 +020012#include <asm/atomic.h>
Christof Schmittdcd20e22009-08-18 15:43:08 +020013#include "zfcp_ext.h"
14#include "zfcp_dbf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Christof Schmitta40a1ba2009-05-15 13:18:16 +020016static unsigned int default_depth = 32;
17module_param_named(queue_depth, default_depth, uint, 0600);
18MODULE_PARM_DESC(queue_depth, "Default queue depth for new SCSI devices");
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020/* Find start of Sense Information in FCP response unit*/
Martin Petermannf76af7d72008-07-02 10:56:36 +020021char *zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 char *fcp_sns_info_ptr;
24
Martin Petermannf76af7d72008-07-02 10:56:36 +020025 fcp_sns_info_ptr = (unsigned char *) &fcp_rsp_iu[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
Martin Petermannf76af7d72008-07-02 10:56:36 +020027 fcp_sns_info_ptr += fcp_rsp_iu->fcp_rsp_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 return fcp_sns_info_ptr;
30}
31
Mike Christiee881a172009-10-15 17:46:39 -070032static int zfcp_scsi_change_queue_depth(struct scsi_device *sdev, int depth,
33 int reason)
Christof Schmitta40a1ba2009-05-15 13:18:16 +020034{
Mike Christiee881a172009-10-15 17:46:39 -070035 if (reason != SCSI_QDEPTH_DEFAULT)
36 return -EOPNOTSUPP;
37
Christof Schmitta40a1ba2009-05-15 13:18:16 +020038 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
39 return sdev->queue_depth;
40}
41
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +020042static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
Christof Schmitt26816f12008-11-04 16:35:05 +010045 unit->device = NULL;
Christof Schmitt26816f12008-11-04 16:35:05 +010046 zfcp_unit_put(unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Martin Petermannf76af7d72008-07-02 10:56:36 +020049static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 if (sdp->tagged_supported)
Christof Schmitta40a1ba2009-05-15 13:18:16 +020052 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, default_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 else
54 scsi_adjust_queue_depth(sdp, 0, 1);
55 return 0;
56}
57
Martin Petermannf76af7d72008-07-02 10:56:36 +020058static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Swen Schillig57717102009-08-18 15:43:21 +020060 struct zfcp_adapter *adapter =
61 (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
Martin Petermannfeac6a02008-07-02 10:56:35 +020062 set_host_byte(scpnt, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020063 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
Swen Schillig57717102009-08-18 15:43:21 +020064 zfcp_dbf_scsi_result("fail", 4, adapter->dbf, scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /* return directly */
66 scpnt->scsi_done(scpnt);
67}
68
Martin Petermannf76af7d72008-07-02 10:56:36 +020069static int zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
70 void (*done) (struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct zfcp_unit *unit;
73 struct zfcp_adapter *adapter;
Christof Schmitta2fa0ae2009-03-02 13:09:08 +010074 int status, scsi_result, ret;
75 struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 /* reset the status for this request */
78 scpnt->result = 0;
79 scpnt->host_scribble = NULL;
80 scpnt->scsi_done = done;
81
82 /*
83 * figure out adapter and target device
84 * (stored there by zfcp_scsi_slave_alloc)
85 */
86 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
Martin Petermannf76af7d72008-07-02 10:56:36 +020087 unit = scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Martin Petermannf76af7d72008-07-02 10:56:36 +020089 BUG_ON(!adapter || (adapter != unit->port->adapter));
90 BUG_ON(!scpnt->scsi_done);
91
92 if (unlikely(!unit)) {
93 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
94 return 0;
95 }
96
Christof Schmitta2fa0ae2009-03-02 13:09:08 +010097 scsi_result = fc_remote_port_chkready(rport);
98 if (unlikely(scsi_result)) {
99 scpnt->result = scsi_result;
Swen Schillig57717102009-08-18 15:43:21 +0200100 zfcp_dbf_scsi_result("fail", 4, adapter->dbf, scpnt, NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100101 scpnt->scsi_done(scpnt);
102 return 0;
103 }
104
Martin Petermannf76af7d72008-07-02 10:56:36 +0200105 status = atomic_read(&unit->status);
106 if (unlikely((status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
107 !(status & ZFCP_STATUS_COMMON_RUNNING))) {
108 zfcp_scsi_command_fail(scpnt, DID_ERROR);
Joe Perchesa419aef2009-08-18 11:18:35 -0700109 return 0;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200110 }
111
Christof Schmitt63caf362009-03-02 13:09:00 +0100112 ret = zfcp_fsf_send_fcp_command_task(unit, scpnt);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200113 if (unlikely(ret == -EBUSY))
Swen Schilligf7a65e92008-11-27 11:44:07 +0100114 return SCSI_MLQUEUE_DEVICE_BUSY;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200115 else if (unlikely(ret < 0))
116 return SCSI_MLQUEUE_HOST_BUSY;
117
118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Martin Petermannf76af7d72008-07-02 10:56:36 +0200121static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *adapter,
122 int channel, unsigned int id,
123 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct zfcp_port *port;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200126 struct zfcp_unit *unit;
Christof Schmitt04062892008-10-01 12:42:20 +0200127 int scsi_lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700130 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 continue;
Christof Schmitt04062892008-10-01 12:42:20 +0200132 list_for_each_entry(unit, &port->unit_list_head, list) {
133 scsi_lun = scsilun_to_int(
134 (struct scsi_lun *)&unit->fcp_lun);
135 if (lun == scsi_lun)
Martin Petermannf76af7d72008-07-02 10:56:36 +0200136 return unit;
Christof Schmitt04062892008-10-01 12:42:20 +0200137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
Martin Petermannf76af7d72008-07-02 10:56:36 +0200139
140 return NULL;
141}
142
143static int zfcp_scsi_slave_alloc(struct scsi_device *sdp)
144{
145 struct zfcp_adapter *adapter;
146 struct zfcp_unit *unit;
147 unsigned long flags;
148 int retval = -ENXIO;
149
150 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
151 if (!adapter)
152 goto out;
153
154 read_lock_irqsave(&zfcp_data.config_lock, flags);
155 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Christof Schmitt86f8a1b2009-03-02 13:08:55 +0100156 if (unit) {
Martin Petermannf76af7d72008-07-02 10:56:36 +0200157 sdp->hostdata = unit;
158 unit->device = sdp;
159 zfcp_unit_get(unit);
160 retval = 0;
161 }
162 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
163out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return retval;
165}
166
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100167static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Christof Schmitt63caf362009-03-02 13:09:00 +0100169 struct Scsi_Host *scsi_host = scpnt->device->host;
170 struct zfcp_adapter *adapter =
171 (struct zfcp_adapter *) scsi_host->hostdata[0];
172 struct zfcp_unit *unit = scpnt->device->hostdata;
173 struct zfcp_fsf_req *old_req, *abrt_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 unsigned long flags;
Christof Schmitta11a52b2009-07-13 15:06:14 +0200175 unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200176 int retval = SUCCESS;
Christof Schmitt63caf362009-03-02 13:09:00 +0100177 int retry = 3;
Christof Schmitta11a52b2009-07-13 15:06:14 +0200178 char *dbf_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200180 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 write_lock_irqsave(&adapter->abort_lock, flags);
182
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200183 spin_lock(&adapter->req_list_lock);
Christof Schmitta11a52b2009-07-13 15:06:14 +0200184 old_req = zfcp_reqlist_find(adapter, old_reqid);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200185 spin_unlock(&adapter->req_list_lock);
Christof Schmitt63caf362009-03-02 13:09:00 +0100186 if (!old_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 write_unlock_irqrestore(&adapter->abort_lock, flags);
Swen Schillig57717102009-08-18 15:43:21 +0200188 zfcp_dbf_scsi_abort("lte1", adapter->dbf, scpnt, NULL,
189 old_reqid);
Christof Schmittc6936e72009-04-17 15:08:11 +0200190 return FAILED; /* completion could be in progress */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100192 old_req->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200194 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200196
Christof Schmitt63caf362009-03-02 13:09:00 +0100197 while (retry--) {
Christof Schmitta11a52b2009-07-13 15:06:14 +0200198 abrt_req = zfcp_fsf_abort_fcp_command(old_reqid, unit);
Christof Schmitt63caf362009-03-02 13:09:00 +0100199 if (abrt_req)
200 break;
201
202 zfcp_erp_wait(adapter);
203 if (!(atomic_read(&adapter->status) &
204 ZFCP_STATUS_COMMON_RUNNING)) {
Swen Schillig57717102009-08-18 15:43:21 +0200205 zfcp_dbf_scsi_abort("nres", adapter->dbf, scpnt, NULL,
206 old_reqid);
Christof Schmitt63caf362009-03-02 13:09:00 +0100207 return SUCCESS;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100210 if (!abrt_req)
211 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Swen Schillig058b8642009-08-18 15:43:14 +0200213 wait_for_completion(&abrt_req->completion);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200214
Christof Schmitt63caf362009-03-02 13:09:00 +0100215 if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
Christof Schmitta11a52b2009-07-13 15:06:14 +0200216 dbf_tag = "okay";
Christof Schmitt63caf362009-03-02 13:09:00 +0100217 else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
Christof Schmitta11a52b2009-07-13 15:06:14 +0200218 dbf_tag = "lte2";
Christof Schmitt63caf362009-03-02 13:09:00 +0100219 else {
Christof Schmitta11a52b2009-07-13 15:06:14 +0200220 dbf_tag = "fail";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Swen Schillig57717102009-08-18 15:43:21 +0200223 zfcp_dbf_scsi_abort(dbf_tag, adapter->dbf, scpnt, abrt_req, old_reqid);
Christof Schmitt63caf362009-03-02 13:09:00 +0100224 zfcp_fsf_req_free(abrt_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return retval;
226}
227
Christof Schmitt63caf362009-03-02 13:09:00 +0100228static int zfcp_task_mgmt_function(struct scsi_cmnd *scpnt, u8 tm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Christof Schmitt63caf362009-03-02 13:09:00 +0100230 struct zfcp_unit *unit = scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct zfcp_adapter *adapter = unit->port->adapter;
Swen Schillig564e1c82009-08-18 15:43:19 +0200232 struct zfcp_fsf_req *fsf_req = NULL;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200233 int retval = SUCCESS;
Christof Schmitt63caf362009-03-02 13:09:00 +0100234 int retry = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Christof Schmitt63caf362009-03-02 13:09:00 +0100236 while (retry--) {
237 fsf_req = zfcp_fsf_send_fcp_ctm(unit, tm_flags);
238 if (fsf_req)
239 break;
240
241 zfcp_erp_wait(adapter);
242 if (!(atomic_read(&adapter->status) &
243 ZFCP_STATUS_COMMON_RUNNING)) {
Swen Schillig57717102009-08-18 15:43:21 +0200244 zfcp_dbf_scsi_devreset("nres", tm_flags, unit, scpnt);
Christof Schmitt63caf362009-03-02 13:09:00 +0100245 return SUCCESS;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100248 if (!fsf_req)
249 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Swen Schillig058b8642009-08-18 15:43:14 +0200251 wait_for_completion(&fsf_req->completion);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200252
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200253 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
Swen Schillig57717102009-08-18 15:43:21 +0200254 zfcp_dbf_scsi_devreset("fail", tm_flags, unit, scpnt);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200255 retval = FAILED;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200256 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
Swen Schillig57717102009-08-18 15:43:21 +0200257 zfcp_dbf_scsi_devreset("nsup", tm_flags, unit, scpnt);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200258 retval = FAILED;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200259 } else
Swen Schillig57717102009-08-18 15:43:21 +0200260 zfcp_dbf_scsi_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200261
262 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return retval;
264}
265
Martin Petermannf76af7d72008-07-02 10:56:36 +0200266static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
267{
Christof Schmitt63caf362009-03-02 13:09:00 +0100268 return zfcp_task_mgmt_function(scpnt, FCP_LOGICAL_UNIT_RESET);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200269}
270
271static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
272{
Christof Schmitt63caf362009-03-02 13:09:00 +0100273 return zfcp_task_mgmt_function(scpnt, FCP_TARGET_RESET);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200274}
275
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100276static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Christof Schmitt63caf362009-03-02 13:09:00 +0100278 struct zfcp_unit *unit = scpnt->device->hostdata;
279 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100281 zfcp_erp_adapter_reopen(adapter, 0, "schrh_1", scpnt);
Andreas Herrmann81654282006-09-18 22:30:36 +0200282 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200284 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Martin Petermannf76af7d72008-07-02 10:56:36 +0200287int zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Martin Petermannf76af7d72008-07-02 10:56:36 +0200289 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Michael Loehr9f287452007-05-09 11:01:24 +0200291 if (adapter->scsi_host)
Martin Petermannf76af7d72008-07-02 10:56:36 +0200292 return 0;
Michael Loehr9f287452007-05-09 11:01:24 +0200293
Martin Petermannf76af7d72008-07-02 10:56:36 +0200294 ccw_device_get_id(adapter->ccw_device, &dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* register adapter as SCSI host with mid layer of SCSI stack */
296 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
297 sizeof (struct zfcp_adapter *));
298 if (!adapter->scsi_host) {
Christof Schmitt553448f2008-06-10 18:20:58 +0200299 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200300 "Registering the FCP device with the "
301 "SCSI stack failed\n");
Martin Petermannf76af7d72008-07-02 10:56:36 +0200302 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* tell the SCSI stack some characteristics of this adapter */
306 adapter->scsi_host->max_id = 1;
307 adapter->scsi_host->max_lun = 1;
308 adapter->scsi_host->max_channel = 0;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200309 adapter->scsi_host->unique_id = dev_id.devno;
310 adapter->scsi_host->max_cmd_len = 255;
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200311 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
314
315 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
316 scsi_host_put(adapter->scsi_host);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200317 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Martin Petermannf76af7d72008-07-02 10:56:36 +0200319
320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Martin Petermannf76af7d72008-07-02 10:56:36 +0200323void zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200326 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 shost = adapter->scsi_host;
329 if (!shost)
330 return;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200331
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200332 read_lock_irq(&zfcp_data.config_lock);
333 list_for_each_entry(port, &adapter->port_list_head, list)
334 if (port->rport)
335 port->rport = NULL;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200336
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200337 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700338 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 scsi_remove_host(shost);
340 scsi_host_put(shost);
341 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 return;
344}
345
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100346static struct fc_host_statistics*
347zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100349 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100351 if (!adapter->fc_stats) {
352 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
353 if (!fc_stats)
354 return NULL;
355 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
356 }
357 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
358 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Martin Petermannf76af7d72008-07-02 10:56:36 +0200361static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
362 struct fsf_qtcb_bottom_port *data,
363 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Martin Petermannf76af7d72008-07-02 10:56:36 +0200365 fc_stats->seconds_since_last_reset =
366 data->seconds_since_last_reset - old->seconds_since_last_reset;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100367 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
368 fc_stats->tx_words = data->tx_words - old->tx_words;
369 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
370 fc_stats->rx_words = data->rx_words - old->rx_words;
371 fc_stats->lip_count = data->lip - old->lip;
372 fc_stats->nos_count = data->nos - old->nos;
373 fc_stats->error_frames = data->error_frames - old->error_frames;
374 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
375 fc_stats->link_failure_count = data->link_failure - old->link_failure;
376 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200377 fc_stats->loss_of_signal_count =
378 data->loss_of_signal - old->loss_of_signal;
379 fc_stats->prim_seq_protocol_err_count =
380 data->psp_error_counts - old->psp_error_counts;
381 fc_stats->invalid_tx_word_count =
382 data->invalid_tx_words - old->invalid_tx_words;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100383 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200384 fc_stats->fcp_input_requests =
385 data->input_requests - old->input_requests;
386 fc_stats->fcp_output_requests =
387 data->output_requests - old->output_requests;
388 fc_stats->fcp_control_requests =
389 data->control_requests - old->control_requests;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100390 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
391 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Martin Petermannf76af7d72008-07-02 10:56:36 +0200394static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
395 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100397 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
398 fc_stats->tx_frames = data->tx_frames;
399 fc_stats->tx_words = data->tx_words;
400 fc_stats->rx_frames = data->rx_frames;
401 fc_stats->rx_words = data->rx_words;
402 fc_stats->lip_count = data->lip;
403 fc_stats->nos_count = data->nos;
404 fc_stats->error_frames = data->error_frames;
405 fc_stats->dumped_frames = data->dumped_frames;
406 fc_stats->link_failure_count = data->link_failure;
407 fc_stats->loss_of_sync_count = data->loss_of_sync;
408 fc_stats->loss_of_signal_count = data->loss_of_signal;
409 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
410 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
411 fc_stats->invalid_crc_count = data->invalid_crcs;
412 fc_stats->fcp_input_requests = data->input_requests;
413 fc_stats->fcp_output_requests = data->output_requests;
414 fc_stats->fcp_control_requests = data->control_requests;
415 fc_stats->fcp_input_megabytes = data->input_mb;
416 fc_stats->fcp_output_megabytes = data->output_mb;
417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Martin Petermannf76af7d72008-07-02 10:56:36 +0200419static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100420{
421 struct zfcp_adapter *adapter;
422 struct fc_host_statistics *fc_stats;
423 struct fsf_qtcb_bottom_port *data;
424 int ret;
425
Martin Petermannf76af7d72008-07-02 10:56:36 +0200426 adapter = (struct zfcp_adapter *)host->hostdata[0];
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100427 fc_stats = zfcp_init_fc_host_stats(adapter);
428 if (!fc_stats)
429 return NULL;
430
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200431 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100432 if (!data)
433 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100434
Swen Schillig564e1c82009-08-18 15:43:19 +0200435 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100436 if (ret) {
437 kfree(data);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200438 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100439 }
440
441 if (adapter->stats_reset &&
442 ((jiffies/HZ - adapter->stats_reset) <
Martin Petermannf76af7d72008-07-02 10:56:36 +0200443 data->seconds_since_last_reset))
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100444 zfcp_adjust_fc_host_stats(fc_stats, data,
445 adapter->stats_reset_data);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200446 else
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100447 zfcp_set_fc_host_stats(fc_stats, data);
448
449 kfree(data);
450 return fc_stats;
451}
452
Martin Petermannf76af7d72008-07-02 10:56:36 +0200453static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100454{
455 struct zfcp_adapter *adapter;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200456 struct fsf_qtcb_bottom_port *data;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100457 int ret;
458
459 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200460 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100461 if (!data)
462 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100463
Swen Schillig564e1c82009-08-18 15:43:19 +0200464 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200465 if (ret)
Heiko Carstens83f6d6d2007-08-08 10:47:02 +0200466 kfree(data);
Martin Petermannf76af7d72008-07-02 10:56:36 +0200467 else {
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100468 adapter->stats_reset = jiffies/HZ;
Martin Petermannf76af7d72008-07-02 10:56:36 +0200469 kfree(adapter->stats_reset_data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100470 adapter->stats_reset_data = data; /* finally freed in
Martin Petermannf76af7d72008-07-02 10:56:36 +0200471 adapter_dequeue */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Sven Schuetz85a82392008-06-10 18:20:59 +0200475static void zfcp_get_host_port_state(struct Scsi_Host *shost)
476{
477 struct zfcp_adapter *adapter =
478 (struct zfcp_adapter *)shost->hostdata[0];
479 int status = atomic_read(&adapter->status);
480
481 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
482 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
483 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
484 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
485 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
486 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
487 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
488 else
489 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
490}
491
Andreas Herrmann338151e2006-05-22 18:25:56 +0200492static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
493{
494 rport->dev_loss_tmo = timeout;
495}
496
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100497/**
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100498 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
499 * @rport: The FC rport where to teminate I/O
500 *
501 * Abort all pending SCSI commands for a port by closing the
502 * port. Using a reopen for avoids a conflict with a shutdown
503 * overwriting a reopen.
504 */
505static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
506{
Christof Schmitt70932932009-04-17 15:08:15 +0200507 struct zfcp_port *port;
Swen Schilligea945ff2009-08-18 15:43:24 +0200508 struct Scsi_Host *shost = rport_to_shost(rport);
509 struct zfcp_adapter *adapter =
510 (struct zfcp_adapter *)shost->hostdata[0];
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100511
Christof Schmitt70932932009-04-17 15:08:15 +0200512 write_lock_irq(&zfcp_data.config_lock);
Swen Schilligea945ff2009-08-18 15:43:24 +0200513 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt70932932009-04-17 15:08:15 +0200514 if (port)
515 zfcp_port_get(port);
516 write_unlock_irq(&zfcp_data.config_lock);
517
518 if (port) {
519 zfcp_erp_port_reopen(port, 0, "sctrpi1", NULL);
520 zfcp_port_put(port);
521 }
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100522}
523
524static void zfcp_scsi_rport_register(struct zfcp_port *port)
525{
526 struct fc_rport_identifiers ids;
527 struct fc_rport *rport;
528
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200529 if (port->rport)
530 return;
531
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100532 ids.node_name = port->wwnn;
533 ids.port_name = port->wwpn;
534 ids.port_id = port->d_id;
535 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
536
537 rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
538 if (!rport) {
539 dev_err(&port->adapter->ccw_device->dev,
540 "Registering port 0x%016Lx failed\n",
541 (unsigned long long)port->wwpn);
542 return;
543 }
544
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100545 rport->maxframe_size = port->maxframe_size;
546 rport->supported_classes = port->supported_classes;
547 port->rport = rport;
548}
549
550static void zfcp_scsi_rport_block(struct zfcp_port *port)
551{
Christof Schmitt70932932009-04-17 15:08:15 +0200552 struct fc_rport *rport = port->rport;
553
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200554 if (rport) {
Christof Schmitt70932932009-04-17 15:08:15 +0200555 fc_remote_port_delete(rport);
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200556 port->rport = NULL;
557 }
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100558}
559
560void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
561{
562 zfcp_port_get(port);
563 port->rport_task = RPORT_ADD;
564
Swen Schillig45446832009-08-18 15:43:17 +0200565 if (!queue_work(port->adapter->work_queue, &port->rport_work))
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100566 zfcp_port_put(port);
567}
568
569void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
570{
571 zfcp_port_get(port);
572 port->rport_task = RPORT_DEL;
573
Swen Schillig45446832009-08-18 15:43:17 +0200574 if (port->rport && queue_work(port->adapter->work_queue,
575 &port->rport_work))
Swen Schilliga67417a2009-08-18 15:43:06 +0200576 return;
577
578 zfcp_port_put(port);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100579}
580
581void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
582{
583 struct zfcp_port *port;
584
585 list_for_each_entry(port, &adapter->port_list_head, list)
586 zfcp_scsi_schedule_rport_block(port);
587}
588
589void zfcp_scsi_rport_work(struct work_struct *work)
590{
591 struct zfcp_port *port = container_of(work, struct zfcp_port,
592 rport_work);
593
594 while (port->rport_task) {
595 if (port->rport_task == RPORT_ADD) {
596 port->rport_task = RPORT_NONE;
597 zfcp_scsi_rport_register(port);
598 } else {
599 port->rport_task = RPORT_NONE;
600 zfcp_scsi_rport_block(port);
601 }
602 }
603
604 zfcp_port_put(port);
605}
606
607
Swen Schillig92d51932009-04-17 15:08:04 +0200608void zfcp_scsi_scan(struct work_struct *work)
609{
610 struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
611 scsi_work);
612 struct fc_rport *rport;
613
614 flush_work(&unit->port->rport_work);
615 rport = unit->port->rport;
616
617 if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
618 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
619 scsilun_to_int((struct scsi_lun *)
620 &unit->fcp_lun), 0);
621
622 zfcp_unit_put(unit);
623}
624
Sven Schuetz9d544f22009-04-06 18:31:47 +0200625static int zfcp_execute_fc_job(struct fc_bsg_job *job)
626{
627 switch (job->request->msgcode) {
628 case FC_BSG_RPT_ELS:
629 case FC_BSG_HST_ELS_NOLOGIN:
630 return zfcp_fc_execute_els_fc_job(job);
631 case FC_BSG_RPT_CT:
632 case FC_BSG_HST_CT:
633 return zfcp_fc_execute_ct_fc_job(job);
634 default:
635 return -EINVAL;
636 }
637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .show_starget_port_id = 1,
641 .show_starget_port_name = 1,
642 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700643 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200644 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200645 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700646 .show_host_node_name = 1,
647 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100648 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700649 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100650 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200651 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700652 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100653 .get_fc_host_stats = zfcp_get_fc_host_stats,
654 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200655 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Sven Schuetz85a82392008-06-10 18:20:59 +0200656 .get_host_port_state = zfcp_get_host_port_state,
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100657 .terminate_rport_io = zfcp_scsi_terminate_rport_io,
Sven Schuetz85a82392008-06-10 18:20:59 +0200658 .show_host_port_state = 1,
Sven Schuetz9d544f22009-04-06 18:31:47 +0200659 .bsg_request = zfcp_execute_fc_job,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100660 /* no functions registered for following dynamic attributes but
661 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100662 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200663 .show_host_speed = 1,
664 .show_host_port_id = 1,
Swen Schillig52ef11a2007-08-28 09:31:09 +0200665 .disable_target_scan = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666};
667
Martin Petermannf76af7d72008-07-02 10:56:36 +0200668struct zfcp_data zfcp_data = {
669 .scsi_host_template = {
670 .name = "zfcp",
671 .module = THIS_MODULE,
672 .proc_name = "zfcp",
Christof Schmitta40a1ba2009-05-15 13:18:16 +0200673 .change_queue_depth = zfcp_scsi_change_queue_depth,
Martin Petermannf76af7d72008-07-02 10:56:36 +0200674 .slave_alloc = zfcp_scsi_slave_alloc,
675 .slave_configure = zfcp_scsi_slave_configure,
676 .slave_destroy = zfcp_scsi_slave_destroy,
677 .queuecommand = zfcp_scsi_queuecommand,
678 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
679 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
680 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
681 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
682 .can_queue = 4096,
683 .this_id = -1,
684 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
685 .cmd_per_lun = 1,
686 .use_clustering = 1,
687 .sdev_attrs = zfcp_sysfs_sdev_attrs,
688 .max_sectors = (ZFCP_MAX_SBALES_PER_REQ * 8),
Swen Schillig60221922008-07-02 10:56:38 +0200689 .shost_attrs = zfcp_sysfs_shost_attrs,
Martin Petermannf76af7d72008-07-02 10:56:36 +0200690 },
691};