blob: f54655998bd5e1c624147b7fcd23fc06d1e68a46 [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 Petermannf76af7d2008-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 Petermannf76af7d2008-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 Petermannf76af7d2008-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{
Christof Schmitt42e62a72009-10-15 17:47:11 -070035 switch (reason) {
36 case SCSI_QDEPTH_DEFAULT:
37 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
38 break;
39 case SCSI_QDEPTH_QFULL:
40 scsi_track_queue_full(sdev, depth);
41 break;
42 case SCSI_QDEPTH_RAMP_UP:
43 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
44 break;
45 default:
Mike Christiee881a172009-10-15 17:46:39 -070046 return -EOPNOTSUPP;
Christof Schmitt42e62a72009-10-15 17:47:11 -070047 }
Christof Schmitta40a1ba2009-05-15 13:18:16 +020048 return sdev->queue_depth;
49}
50
Andreas Herrmannf6c0e7a2006-08-02 11:05:52 +020051static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
Christof Schmitt26816f12008-11-04 16:35:05 +010054 unit->device = NULL;
Christof Schmitt26816f12008-11-04 16:35:05 +010055 zfcp_unit_put(unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Martin Petermannf76af7d2008-07-02 10:56:36 +020058static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 if (sdp->tagged_supported)
Christof Schmitta40a1ba2009-05-15 13:18:16 +020061 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, default_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 else
63 scsi_adjust_queue_depth(sdp, 0, 1);
64 return 0;
65}
66
Martin Petermannf76af7d2008-07-02 10:56:36 +020067static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Swen Schillig57717102009-08-18 15:43:21 +020069 struct zfcp_adapter *adapter =
70 (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
Martin Petermannfeac6a02008-07-02 10:56:35 +020071 set_host_byte(scpnt, result);
Maxim Shchetynin8a36e452005-09-13 21:50:38 +020072 if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
Swen Schillig57717102009-08-18 15:43:21 +020073 zfcp_dbf_scsi_result("fail", 4, adapter->dbf, scpnt, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /* return directly */
75 scpnt->scsi_done(scpnt);
76}
77
Martin Petermannf76af7d2008-07-02 10:56:36 +020078static int zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
79 void (*done) (struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct zfcp_unit *unit;
82 struct zfcp_adapter *adapter;
Christof Schmitta2fa0ae2009-03-02 13:09:08 +010083 int status, scsi_result, ret;
84 struct fc_rport *rport = starget_to_rport(scsi_target(scpnt->device));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* reset the status for this request */
87 scpnt->result = 0;
88 scpnt->host_scribble = NULL;
89 scpnt->scsi_done = done;
90
91 /*
92 * figure out adapter and target device
93 * (stored there by zfcp_scsi_slave_alloc)
94 */
95 adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
Martin Petermannf76af7d2008-07-02 10:56:36 +020096 unit = scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Martin Petermannf76af7d2008-07-02 10:56:36 +020098 BUG_ON(!adapter || (adapter != unit->port->adapter));
99 BUG_ON(!scpnt->scsi_done);
100
101 if (unlikely(!unit)) {
102 zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
103 return 0;
104 }
105
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100106 scsi_result = fc_remote_port_chkready(rport);
107 if (unlikely(scsi_result)) {
108 scpnt->result = scsi_result;
Swen Schillig57717102009-08-18 15:43:21 +0200109 zfcp_dbf_scsi_result("fail", 4, adapter->dbf, scpnt, NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100110 scpnt->scsi_done(scpnt);
111 return 0;
112 }
113
Martin Petermannf76af7d2008-07-02 10:56:36 +0200114 status = atomic_read(&unit->status);
115 if (unlikely((status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
116 !(status & ZFCP_STATUS_COMMON_RUNNING))) {
117 zfcp_scsi_command_fail(scpnt, DID_ERROR);
Joe Perchesa419aef2009-08-18 11:18:35 -0700118 return 0;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200119 }
120
Christof Schmitt63caf362009-03-02 13:09:00 +0100121 ret = zfcp_fsf_send_fcp_command_task(unit, scpnt);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200122 if (unlikely(ret == -EBUSY))
Swen Schilligf7a65e92008-11-27 11:44:07 +0100123 return SCSI_MLQUEUE_DEVICE_BUSY;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200124 else if (unlikely(ret < 0))
125 return SCSI_MLQUEUE_HOST_BUSY;
126
127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Martin Petermannf76af7d2008-07-02 10:56:36 +0200130static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *adapter,
131 int channel, unsigned int id,
132 unsigned int lun)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct zfcp_port *port;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200135 struct zfcp_unit *unit;
Christof Schmitt04062892008-10-01 12:42:20 +0200136 int scsi_lun;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 list_for_each_entry(port, &adapter->port_list_head, list) {
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700139 if (!port->rport || (id != port->rport->scsi_target_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 continue;
Christof Schmitt04062892008-10-01 12:42:20 +0200141 list_for_each_entry(unit, &port->unit_list_head, list) {
142 scsi_lun = scsilun_to_int(
143 (struct scsi_lun *)&unit->fcp_lun);
144 if (lun == scsi_lun)
Martin Petermannf76af7d2008-07-02 10:56:36 +0200145 return unit;
Christof Schmitt04062892008-10-01 12:42:20 +0200146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Martin Petermannf76af7d2008-07-02 10:56:36 +0200148
149 return NULL;
150}
151
152static int zfcp_scsi_slave_alloc(struct scsi_device *sdp)
153{
154 struct zfcp_adapter *adapter;
155 struct zfcp_unit *unit;
156 unsigned long flags;
157 int retval = -ENXIO;
158
159 adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
160 if (!adapter)
161 goto out;
162
163 read_lock_irqsave(&zfcp_data.config_lock, flags);
164 unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
Christof Schmitt86f8a1b2009-03-02 13:08:55 +0100165 if (unit) {
Martin Petermannf76af7d2008-07-02 10:56:36 +0200166 sdp->hostdata = unit;
167 unit->device = sdp;
168 zfcp_unit_get(unit);
169 retval = 0;
170 }
171 read_unlock_irqrestore(&zfcp_data.config_lock, flags);
172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return retval;
174}
175
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100176static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Christof Schmitt63caf362009-03-02 13:09:00 +0100178 struct Scsi_Host *scsi_host = scpnt->device->host;
179 struct zfcp_adapter *adapter =
180 (struct zfcp_adapter *) scsi_host->hostdata[0];
181 struct zfcp_unit *unit = scpnt->device->hostdata;
182 struct zfcp_fsf_req *old_req, *abrt_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned long flags;
Christof Schmitta11a52b2009-07-13 15:06:14 +0200184 unsigned long old_reqid = (unsigned long) scpnt->host_scribble;
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200185 int retval = SUCCESS;
Christof Schmitt63caf362009-03-02 13:09:00 +0100186 int retry = 3;
Christof Schmitta11a52b2009-07-13 15:06:14 +0200187 char *dbf_tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200189 /* avoid race condition between late normal completion and abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 write_lock_irqsave(&adapter->abort_lock, flags);
191
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200192 spin_lock(&adapter->req_list_lock);
Christof Schmitta11a52b2009-07-13 15:06:14 +0200193 old_req = zfcp_reqlist_find(adapter, old_reqid);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200194 spin_unlock(&adapter->req_list_lock);
Christof Schmitt63caf362009-03-02 13:09:00 +0100195 if (!old_req) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 write_unlock_irqrestore(&adapter->abort_lock, flags);
Swen Schillig57717102009-08-18 15:43:21 +0200197 zfcp_dbf_scsi_abort("lte1", adapter->dbf, scpnt, NULL,
198 old_reqid);
Christof Schmittc6936e72009-04-17 15:08:11 +0200199 return FAILED; /* completion could be in progress */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100201 old_req->data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200203 /* don't access old fsf_req after releasing the abort_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 write_unlock_irqrestore(&adapter->abort_lock, flags);
Andreas Herrmann4eff4a32006-09-18 22:29:20 +0200205
Christof Schmitt63caf362009-03-02 13:09:00 +0100206 while (retry--) {
Christof Schmitta11a52b2009-07-13 15:06:14 +0200207 abrt_req = zfcp_fsf_abort_fcp_command(old_reqid, unit);
Christof Schmitt63caf362009-03-02 13:09:00 +0100208 if (abrt_req)
209 break;
210
211 zfcp_erp_wait(adapter);
212 if (!(atomic_read(&adapter->status) &
213 ZFCP_STATUS_COMMON_RUNNING)) {
Swen Schillig57717102009-08-18 15:43:21 +0200214 zfcp_dbf_scsi_abort("nres", adapter->dbf, scpnt, NULL,
215 old_reqid);
Christof Schmitt63caf362009-03-02 13:09:00 +0100216 return SUCCESS;
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100219 if (!abrt_req)
220 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Swen Schillig058b8642009-08-18 15:43:14 +0200222 wait_for_completion(&abrt_req->completion);
Andreas Herrmann059c97d2005-09-13 21:47:52 +0200223
Christof Schmitt63caf362009-03-02 13:09:00 +0100224 if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED)
Christof Schmitta11a52b2009-07-13 15:06:14 +0200225 dbf_tag = "okay";
Christof Schmitt63caf362009-03-02 13:09:00 +0100226 else if (abrt_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED)
Christof Schmitta11a52b2009-07-13 15:06:14 +0200227 dbf_tag = "lte2";
Christof Schmitt63caf362009-03-02 13:09:00 +0100228 else {
Christof Schmitta11a52b2009-07-13 15:06:14 +0200229 dbf_tag = "fail";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 retval = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Swen Schillig57717102009-08-18 15:43:21 +0200232 zfcp_dbf_scsi_abort(dbf_tag, adapter->dbf, scpnt, abrt_req, old_reqid);
Christof Schmitt63caf362009-03-02 13:09:00 +0100233 zfcp_fsf_req_free(abrt_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return retval;
235}
236
Christof Schmitt63caf362009-03-02 13:09:00 +0100237static int zfcp_task_mgmt_function(struct scsi_cmnd *scpnt, u8 tm_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Christof Schmitt63caf362009-03-02 13:09:00 +0100239 struct zfcp_unit *unit = scpnt->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 struct zfcp_adapter *adapter = unit->port->adapter;
Swen Schillig564e1c82009-08-18 15:43:19 +0200241 struct zfcp_fsf_req *fsf_req = NULL;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200242 int retval = SUCCESS;
Christof Schmitt63caf362009-03-02 13:09:00 +0100243 int retry = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Christof Schmitt63caf362009-03-02 13:09:00 +0100245 while (retry--) {
246 fsf_req = zfcp_fsf_send_fcp_ctm(unit, tm_flags);
247 if (fsf_req)
248 break;
249
250 zfcp_erp_wait(adapter);
251 if (!(atomic_read(&adapter->status) &
252 ZFCP_STATUS_COMMON_RUNNING)) {
Swen Schillig57717102009-08-18 15:43:21 +0200253 zfcp_dbf_scsi_devreset("nres", tm_flags, unit, scpnt);
Christof Schmitt63caf362009-03-02 13:09:00 +0100254 return SUCCESS;
255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Christof Schmitt63caf362009-03-02 13:09:00 +0100257 if (!fsf_req)
258 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Swen Schillig058b8642009-08-18 15:43:14 +0200260 wait_for_completion(&fsf_req->completion);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200261
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200262 if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
Swen Schillig57717102009-08-18 15:43:21 +0200263 zfcp_dbf_scsi_devreset("fail", tm_flags, unit, scpnt);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200264 retval = FAILED;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200265 } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
Swen Schillig57717102009-08-18 15:43:21 +0200266 zfcp_dbf_scsi_devreset("nsup", tm_flags, unit, scpnt);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200267 retval = FAILED;
Maxim Shchetynin8a36e452005-09-13 21:50:38 +0200268 } else
Swen Schillig57717102009-08-18 15:43:21 +0200269 zfcp_dbf_scsi_devreset("okay", tm_flags, unit, scpnt);
Andreas Herrmann77eb1692005-09-13 21:48:33 +0200270
271 zfcp_fsf_req_free(fsf_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return retval;
273}
274
Martin Petermannf76af7d2008-07-02 10:56:36 +0200275static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
276{
Christof Schmitt63caf362009-03-02 13:09:00 +0100277 return zfcp_task_mgmt_function(scpnt, FCP_LOGICAL_UNIT_RESET);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200278}
279
280static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
281{
Christof Schmitt63caf362009-03-02 13:09:00 +0100282 return zfcp_task_mgmt_function(scpnt, FCP_TARGET_RESET);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200283}
284
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100285static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Christof Schmitt63caf362009-03-02 13:09:00 +0100287 struct zfcp_unit *unit = scpnt->device->hostdata;
288 struct zfcp_adapter *adapter = unit->port->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100290 zfcp_erp_adapter_reopen(adapter, 0, "schrh_1", scpnt);
Andreas Herrmann81654282006-09-18 22:30:36 +0200291 zfcp_erp_wait(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Andreas Herrmann810f1e3e2005-09-13 21:49:52 +0200293 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Martin Petermannf76af7d2008-07-02 10:56:36 +0200296int zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Martin Petermannf76af7d2008-07-02 10:56:36 +0200298 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Michael Loehr9f287452007-05-09 11:01:24 +0200300 if (adapter->scsi_host)
Martin Petermannf76af7d2008-07-02 10:56:36 +0200301 return 0;
Michael Loehr9f287452007-05-09 11:01:24 +0200302
Martin Petermannf76af7d2008-07-02 10:56:36 +0200303 ccw_device_get_id(adapter->ccw_device, &dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* register adapter as SCSI host with mid layer of SCSI stack */
305 adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
306 sizeof (struct zfcp_adapter *));
307 if (!adapter->scsi_host) {
Christof Schmitt553448f2008-06-10 18:20:58 +0200308 dev_err(&adapter->ccw_device->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200309 "Registering the FCP device with the "
310 "SCSI stack failed\n");
Martin Petermannf76af7d2008-07-02 10:56:36 +0200311 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* tell the SCSI stack some characteristics of this adapter */
315 adapter->scsi_host->max_id = 1;
316 adapter->scsi_host->max_lun = 1;
317 adapter->scsi_host->max_channel = 0;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200318 adapter->scsi_host->unique_id = dev_id.devno;
319 adapter->scsi_host->max_cmd_len = 255;
Heiko Carstensdd52e0e2006-09-18 22:28:49 +0200320 adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
323
324 if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
325 scsi_host_put(adapter->scsi_host);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200326 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Martin Petermannf76af7d2008-07-02 10:56:36 +0200328
329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Martin Petermannf76af7d2008-07-02 10:56:36 +0200332void zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct Scsi_Host *shost;
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200335 struct zfcp_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 shost = adapter->scsi_host;
338 if (!shost)
339 return;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200340
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200341 read_lock_irq(&zfcp_data.config_lock);
342 list_for_each_entry(port, &adapter->port_list_head, list)
343 if (port->rport)
344 port->rport = NULL;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200345
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200346 read_unlock_irq(&zfcp_data.config_lock);
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700347 fc_remove_host(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 scsi_remove_host(shost);
349 scsi_host_put(shost);
350 adapter->scsi_host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 return;
353}
354
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100355static struct fc_host_statistics*
356zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100358 struct fc_host_statistics *fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100360 if (!adapter->fc_stats) {
361 fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
362 if (!fc_stats)
363 return NULL;
364 adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
365 }
366 memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
367 return adapter->fc_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Martin Petermannf76af7d2008-07-02 10:56:36 +0200370static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
371 struct fsf_qtcb_bottom_port *data,
372 struct fsf_qtcb_bottom_port *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Martin Petermannf76af7d2008-07-02 10:56:36 +0200374 fc_stats->seconds_since_last_reset =
375 data->seconds_since_last_reset - old->seconds_since_last_reset;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100376 fc_stats->tx_frames = data->tx_frames - old->tx_frames;
377 fc_stats->tx_words = data->tx_words - old->tx_words;
378 fc_stats->rx_frames = data->rx_frames - old->rx_frames;
379 fc_stats->rx_words = data->rx_words - old->rx_words;
380 fc_stats->lip_count = data->lip - old->lip;
381 fc_stats->nos_count = data->nos - old->nos;
382 fc_stats->error_frames = data->error_frames - old->error_frames;
383 fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
384 fc_stats->link_failure_count = data->link_failure - old->link_failure;
385 fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200386 fc_stats->loss_of_signal_count =
387 data->loss_of_signal - old->loss_of_signal;
388 fc_stats->prim_seq_protocol_err_count =
389 data->psp_error_counts - old->psp_error_counts;
390 fc_stats->invalid_tx_word_count =
391 data->invalid_tx_words - old->invalid_tx_words;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100392 fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200393 fc_stats->fcp_input_requests =
394 data->input_requests - old->input_requests;
395 fc_stats->fcp_output_requests =
396 data->output_requests - old->output_requests;
397 fc_stats->fcp_control_requests =
398 data->control_requests - old->control_requests;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100399 fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
400 fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Martin Petermannf76af7d2008-07-02 10:56:36 +0200403static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
404 struct fsf_qtcb_bottom_port *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100406 fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
407 fc_stats->tx_frames = data->tx_frames;
408 fc_stats->tx_words = data->tx_words;
409 fc_stats->rx_frames = data->rx_frames;
410 fc_stats->rx_words = data->rx_words;
411 fc_stats->lip_count = data->lip;
412 fc_stats->nos_count = data->nos;
413 fc_stats->error_frames = data->error_frames;
414 fc_stats->dumped_frames = data->dumped_frames;
415 fc_stats->link_failure_count = data->link_failure;
416 fc_stats->loss_of_sync_count = data->loss_of_sync;
417 fc_stats->loss_of_signal_count = data->loss_of_signal;
418 fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
419 fc_stats->invalid_tx_word_count = data->invalid_tx_words;
420 fc_stats->invalid_crc_count = data->invalid_crcs;
421 fc_stats->fcp_input_requests = data->input_requests;
422 fc_stats->fcp_output_requests = data->output_requests;
423 fc_stats->fcp_control_requests = data->control_requests;
424 fc_stats->fcp_input_megabytes = data->input_mb;
425 fc_stats->fcp_output_megabytes = data->output_mb;
426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Martin Petermannf76af7d2008-07-02 10:56:36 +0200428static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100429{
430 struct zfcp_adapter *adapter;
431 struct fc_host_statistics *fc_stats;
432 struct fsf_qtcb_bottom_port *data;
433 int ret;
434
Martin Petermannf76af7d2008-07-02 10:56:36 +0200435 adapter = (struct zfcp_adapter *)host->hostdata[0];
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100436 fc_stats = zfcp_init_fc_host_stats(adapter);
437 if (!fc_stats)
438 return NULL;
439
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200440 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100441 if (!data)
442 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100443
Swen Schillig564e1c82009-08-18 15:43:19 +0200444 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100445 if (ret) {
446 kfree(data);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200447 return NULL;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100448 }
449
450 if (adapter->stats_reset &&
451 ((jiffies/HZ - adapter->stats_reset) <
Martin Petermannf76af7d2008-07-02 10:56:36 +0200452 data->seconds_since_last_reset))
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100453 zfcp_adjust_fc_host_stats(fc_stats, data,
454 adapter->stats_reset_data);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200455 else
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100456 zfcp_set_fc_host_stats(fc_stats, data);
457
458 kfree(data);
459 return fc_stats;
460}
461
Martin Petermannf76af7d2008-07-02 10:56:36 +0200462static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100463{
464 struct zfcp_adapter *adapter;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200465 struct fsf_qtcb_bottom_port *data;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100466 int ret;
467
468 adapter = (struct zfcp_adapter *)shost->hostdata[0];
Andreas Herrmannec4081c2006-05-22 18:17:30 +0200469 data = kzalloc(sizeof(*data), GFP_KERNEL);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100470 if (!data)
471 return;
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100472
Swen Schillig564e1c82009-08-18 15:43:19 +0200473 ret = zfcp_fsf_exchange_port_data_sync(adapter->qdio, data);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200474 if (ret)
Heiko Carstens83f6d6d2007-08-08 10:47:02 +0200475 kfree(data);
Martin Petermannf76af7d2008-07-02 10:56:36 +0200476 else {
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100477 adapter->stats_reset = jiffies/HZ;
Martin Petermannf76af7d2008-07-02 10:56:36 +0200478 kfree(adapter->stats_reset_data);
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100479 adapter->stats_reset_data = data; /* finally freed in
Martin Petermannf76af7d2008-07-02 10:56:36 +0200480 adapter_dequeue */
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Sven Schuetz85a82392008-06-10 18:20:59 +0200484static void zfcp_get_host_port_state(struct Scsi_Host *shost)
485{
486 struct zfcp_adapter *adapter =
487 (struct zfcp_adapter *)shost->hostdata[0];
488 int status = atomic_read(&adapter->status);
489
490 if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
491 !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
492 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
493 else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
494 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
495 else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
496 fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
497 else
498 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
499}
500
Andreas Herrmann338151e2006-05-22 18:25:56 +0200501static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
502{
503 rport->dev_loss_tmo = timeout;
504}
505
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100506/**
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100507 * zfcp_scsi_terminate_rport_io - Terminate all I/O on a rport
508 * @rport: The FC rport where to teminate I/O
509 *
510 * Abort all pending SCSI commands for a port by closing the
511 * port. Using a reopen for avoids a conflict with a shutdown
512 * overwriting a reopen.
513 */
514static void zfcp_scsi_terminate_rport_io(struct fc_rport *rport)
515{
Christof Schmitt70932932009-04-17 15:08:15 +0200516 struct zfcp_port *port;
Swen Schilligea945ff2009-08-18 15:43:24 +0200517 struct Scsi_Host *shost = rport_to_shost(rport);
518 struct zfcp_adapter *adapter =
519 (struct zfcp_adapter *)shost->hostdata[0];
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100520
Christof Schmitt70932932009-04-17 15:08:15 +0200521 write_lock_irq(&zfcp_data.config_lock);
Swen Schilligea945ff2009-08-18 15:43:24 +0200522 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt70932932009-04-17 15:08:15 +0200523 if (port)
524 zfcp_port_get(port);
525 write_unlock_irq(&zfcp_data.config_lock);
526
527 if (port) {
528 zfcp_erp_port_reopen(port, 0, "sctrpi1", NULL);
529 zfcp_port_put(port);
530 }
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100531}
532
533static void zfcp_scsi_rport_register(struct zfcp_port *port)
534{
535 struct fc_rport_identifiers ids;
536 struct fc_rport *rport;
537
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200538 if (port->rport)
539 return;
540
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100541 ids.node_name = port->wwnn;
542 ids.port_name = port->wwpn;
543 ids.port_id = port->d_id;
544 ids.roles = FC_RPORT_ROLE_FCP_TARGET;
545
546 rport = fc_remote_port_add(port->adapter->scsi_host, 0, &ids);
547 if (!rport) {
548 dev_err(&port->adapter->ccw_device->dev,
549 "Registering port 0x%016Lx failed\n",
550 (unsigned long long)port->wwpn);
551 return;
552 }
553
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100554 rport->maxframe_size = port->maxframe_size;
555 rport->supported_classes = port->supported_classes;
556 port->rport = rport;
557}
558
559static void zfcp_scsi_rport_block(struct zfcp_port *port)
560{
Christof Schmitt70932932009-04-17 15:08:15 +0200561 struct fc_rport *rport = port->rport;
562
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200563 if (rport) {
Christof Schmitt70932932009-04-17 15:08:15 +0200564 fc_remote_port_delete(rport);
Christof Schmitt379d6bf2009-07-13 15:06:11 +0200565 port->rport = NULL;
566 }
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100567}
568
569void zfcp_scsi_schedule_rport_register(struct zfcp_port *port)
570{
571 zfcp_port_get(port);
572 port->rport_task = RPORT_ADD;
573
Swen Schillig45446832009-08-18 15:43:17 +0200574 if (!queue_work(port->adapter->work_queue, &port->rport_work))
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100575 zfcp_port_put(port);
576}
577
578void zfcp_scsi_schedule_rport_block(struct zfcp_port *port)
579{
580 zfcp_port_get(port);
581 port->rport_task = RPORT_DEL;
582
Swen Schillig45446832009-08-18 15:43:17 +0200583 if (port->rport && queue_work(port->adapter->work_queue,
584 &port->rport_work))
Swen Schilliga67417a2009-08-18 15:43:06 +0200585 return;
586
587 zfcp_port_put(port);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100588}
589
590void zfcp_scsi_schedule_rports_block(struct zfcp_adapter *adapter)
591{
592 struct zfcp_port *port;
593
594 list_for_each_entry(port, &adapter->port_list_head, list)
595 zfcp_scsi_schedule_rport_block(port);
596}
597
598void zfcp_scsi_rport_work(struct work_struct *work)
599{
600 struct zfcp_port *port = container_of(work, struct zfcp_port,
601 rport_work);
602
603 while (port->rport_task) {
604 if (port->rport_task == RPORT_ADD) {
605 port->rport_task = RPORT_NONE;
606 zfcp_scsi_rport_register(port);
607 } else {
608 port->rport_task = RPORT_NONE;
609 zfcp_scsi_rport_block(port);
610 }
611 }
612
613 zfcp_port_put(port);
614}
615
616
Swen Schillig92d51932009-04-17 15:08:04 +0200617void zfcp_scsi_scan(struct work_struct *work)
618{
619 struct zfcp_unit *unit = container_of(work, struct zfcp_unit,
620 scsi_work);
621 struct fc_rport *rport;
622
623 flush_work(&unit->port->rport_work);
624 rport = unit->port->rport;
625
626 if (rport && rport->port_state == FC_PORTSTATE_ONLINE)
627 scsi_scan_target(&rport->dev, 0, rport->scsi_target_id,
628 scsilun_to_int((struct scsi_lun *)
629 &unit->fcp_lun), 0);
630
631 zfcp_unit_put(unit);
632}
633
Sven Schuetz9d544f22009-04-06 18:31:47 +0200634static int zfcp_execute_fc_job(struct fc_bsg_job *job)
635{
636 switch (job->request->msgcode) {
637 case FC_BSG_RPT_ELS:
638 case FC_BSG_HST_ELS_NOLOGIN:
639 return zfcp_fc_execute_els_fc_job(job);
640 case FC_BSG_RPT_CT:
641 case FC_BSG_HST_CT:
642 return zfcp_fc_execute_ct_fc_job(job);
643 default:
644 return -EINVAL;
645 }
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648struct fc_function_template zfcp_transport_functions = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .show_starget_port_id = 1,
650 .show_starget_port_name = 1,
651 .show_starget_node_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700652 .show_rport_supported_classes = 1,
Ralph Wuerthner75bfc282006-05-22 18:24:33 +0200653 .show_rport_maxframe_size = 1,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200654 .show_rport_dev_loss_tmo = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700655 .show_host_node_name = 1,
656 .show_host_port_name = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100657 .show_host_permanent_port_name = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700658 .show_host_supported_classes = 1,
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100659 .show_host_supported_speeds = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200660 .show_host_maxframe_size = 1,
Andreas Herrmann3859f6a2005-08-27 11:07:54 -0700661 .show_host_serial_number = 1,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100662 .get_fc_host_stats = zfcp_get_fc_host_stats,
663 .reset_fc_host_stats = zfcp_reset_fc_host_stats,
Andreas Herrmann338151e2006-05-22 18:25:56 +0200664 .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
Sven Schuetz85a82392008-06-10 18:20:59 +0200665 .get_host_port_state = zfcp_get_host_port_state,
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100666 .terminate_rport_io = zfcp_scsi_terminate_rport_io,
Sven Schuetz85a82392008-06-10 18:20:59 +0200667 .show_host_port_state = 1,
Sven Schuetz9d544f22009-04-06 18:31:47 +0200668 .bsg_request = zfcp_execute_fc_job,
Andreas Herrmannf6cd94b2006-01-05 09:59:34 +0100669 /* no functions registered for following dynamic attributes but
670 directly set by LLDD */
Andreas Herrmannad757cd2006-01-13 02:26:11 +0100671 .show_host_port_type = 1,
Andreas Herrmann13e1e1f2005-09-19 16:56:17 +0200672 .show_host_speed = 1,
673 .show_host_port_id = 1,
Swen Schillig52ef11a2007-08-28 09:31:09 +0200674 .disable_target_scan = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675};
676
Martin Petermannf76af7d2008-07-02 10:56:36 +0200677struct zfcp_data zfcp_data = {
678 .scsi_host_template = {
679 .name = "zfcp",
680 .module = THIS_MODULE,
681 .proc_name = "zfcp",
Christof Schmitta40a1ba2009-05-15 13:18:16 +0200682 .change_queue_depth = zfcp_scsi_change_queue_depth,
Martin Petermannf76af7d2008-07-02 10:56:36 +0200683 .slave_alloc = zfcp_scsi_slave_alloc,
684 .slave_configure = zfcp_scsi_slave_configure,
685 .slave_destroy = zfcp_scsi_slave_destroy,
686 .queuecommand = zfcp_scsi_queuecommand,
687 .eh_abort_handler = zfcp_scsi_eh_abort_handler,
688 .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
689 .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
690 .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
691 .can_queue = 4096,
692 .this_id = -1,
693 .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
694 .cmd_per_lun = 1,
695 .use_clustering = 1,
696 .sdev_attrs = zfcp_sysfs_sdev_attrs,
697 .max_sectors = (ZFCP_MAX_SBALES_PER_REQ * 8),
Swen Schillig60221922008-07-02 10:56:38 +0200698 .shost_attrs = zfcp_sysfs_shost_attrs,
Martin Petermannf76af7d2008-07-02 10:56:36 +0200699 },
700};