blob: 7c306a5ef4dd56c691bdec6eb251b5bdf2c70e57 [file] [log] [blame]
Christof Schmitt24073b42008-06-10 18:20:54 +02001/*
2 * zfcp device driver
3 *
4 * Fibre Channel related functions for the zfcp device driver.
5 *
Christof Schmitta2fa0ae2009-03-02 13:09:08 +01006 * Copyright IBM Corporation 2008, 2009
Christof Schmitt24073b42008-06-10 18:20:54 +02007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Christof Schmitt9d05ce22009-11-24 16:54:09 +010012#include <linux/types.h>
13#include <scsi/fc/fc_els.h>
14#include <scsi/libfc.h>
Christof Schmitt24073b42008-06-10 18:20:54 +020015#include "zfcp_ext.h"
Christof Schmitt9d05ce22009-11-24 16:54:09 +010016#include "zfcp_fc.h"
Christof Schmitt24073b42008-06-10 18:20:54 +020017
Christof Schmitt9d05ce22009-11-24 16:54:09 +010018static u32 zfcp_fc_rscn_range_mask[] = {
19 [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
20 [ELS_ADDR_FMT_AREA] = 0xFFFF00,
21 [ELS_ADDR_FMT_DOM] = 0xFF0000,
22 [ELS_ADDR_FMT_FAB] = 0x000000,
Christof Schmitte0d7fcb2008-12-19 16:56:58 +010023};
24
Swen Schillig5ab944f2008-10-01 12:42:17 +020025struct zfcp_fc_ns_handler_data {
26 struct completion done;
27 void (*handler)(unsigned long);
28 unsigned long handler_data;
29};
30
Swen Schillig6f53a2d2009-08-18 15:43:23 +020031static int zfcp_fc_wka_port_get(struct zfcp_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020032{
33 if (mutex_lock_interruptible(&wka_port->mutex))
34 return -ERESTARTSYS;
35
Christof Schmitt1c1cba12008-11-26 18:07:36 +010036 if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
37 wka_port->status == ZFCP_WKA_PORT_CLOSING) {
Swen Schillig5ab944f2008-10-01 12:42:17 +020038 wka_port->status = ZFCP_WKA_PORT_OPENING;
39 if (zfcp_fsf_open_wka_port(wka_port))
40 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
41 }
42
43 mutex_unlock(&wka_port->mutex);
44
Swen Schillig27f492c2009-07-13 15:06:13 +020045 wait_event(wka_port->completion_wq,
46 wka_port->status == ZFCP_WKA_PORT_ONLINE ||
47 wka_port->status == ZFCP_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +020048
49 if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
50 atomic_inc(&wka_port->refcount);
51 return 0;
52 }
53 return -EIO;
54}
55
Swen Schillig6f53a2d2009-08-18 15:43:23 +020056static void zfcp_fc_wka_port_offline(struct work_struct *work)
Swen Schillig5ab944f2008-10-01 12:42:17 +020057{
Jean Delvarebf6aede2009-04-02 16:56:54 -070058 struct delayed_work *dw = to_delayed_work(work);
Swen Schillig5ab944f2008-10-01 12:42:17 +020059 struct zfcp_wka_port *wka_port =
60 container_of(dw, struct zfcp_wka_port, work);
61
Swen Schillig5ab944f2008-10-01 12:42:17 +020062 mutex_lock(&wka_port->mutex);
63 if ((atomic_read(&wka_port->refcount) != 0) ||
64 (wka_port->status != ZFCP_WKA_PORT_ONLINE))
65 goto out;
66
67 wka_port->status = ZFCP_WKA_PORT_CLOSING;
68 if (zfcp_fsf_close_wka_port(wka_port)) {
69 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
70 wake_up(&wka_port->completion_wq);
71 }
72out:
73 mutex_unlock(&wka_port->mutex);
74}
75
Swen Schillig6f53a2d2009-08-18 15:43:23 +020076static void zfcp_fc_wka_port_put(struct zfcp_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020077{
78 if (atomic_dec_return(&wka_port->refcount) != 0)
79 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +020080 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +020081 schedule_delayed_work(&wka_port->work, HZ / 100);
82}
83
Sven Schuetz9d544f22009-04-06 18:31:47 +020084static void zfcp_fc_wka_port_init(struct zfcp_wka_port *wka_port, u32 d_id,
85 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +020086{
Swen Schillig5ab944f2008-10-01 12:42:17 +020087 init_waitqueue_head(&wka_port->completion_wq);
88
89 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +020090 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +020091
92 wka_port->status = ZFCP_WKA_PORT_OFFLINE;
93 atomic_set(&wka_port->refcount, 0);
94 mutex_init(&wka_port->mutex);
Swen Schillig6f53a2d2009-08-18 15:43:23 +020095 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
Swen Schillig5ab944f2008-10-01 12:42:17 +020096}
97
Christof Schmitt55c770f2009-08-18 15:43:12 +020098static void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
Swen Schillig828bc122009-04-17 15:08:05 +020099{
100 cancel_delayed_work_sync(&wka->work);
101 mutex_lock(&wka->mutex);
102 wka->status = ZFCP_WKA_PORT_OFFLINE;
103 mutex_unlock(&wka->mutex);
104}
105
Christof Schmitt55c770f2009-08-18 15:43:12 +0200106void zfcp_fc_wka_ports_force_offline(struct zfcp_wka_ports *gs)
107{
Swen Schilligf3450c72009-11-24 16:53:59 +0100108 if (!gs)
109 return;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200110 zfcp_fc_wka_port_force_offline(&gs->ms);
111 zfcp_fc_wka_port_force_offline(&gs->ts);
112 zfcp_fc_wka_port_force_offline(&gs->ds);
113 zfcp_fc_wka_port_force_offline(&gs->as);
114 zfcp_fc_wka_port_force_offline(&gs->ks);
115}
116
Christof Schmitt24073b42008-06-10 18:20:54 +0200117static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100118 struct fc_els_rscn_page *page)
Christof Schmitt24073b42008-06-10 18:20:54 +0200119{
120 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +0100121 struct zfcp_adapter *adapter = fsf_req->adapter;
Christof Schmitt24073b42008-06-10 18:20:54 +0200122 struct zfcp_port *port;
123
Swen Schilligecf0c772009-11-24 16:53:58 +0100124 read_lock_irqsave(&adapter->port_list_lock, flags);
125 list_for_each_entry(port, &adapter->port_list, list) {
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100126 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200127 zfcp_fc_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200128 if (!port->d_id)
129 zfcp_erp_port_reopen(port,
130 ZFCP_STATUS_COMMON_ERP_FAILED,
131 "fcrscn1", NULL);
132 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100133 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200134}
135
136static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
137{
138 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100139 struct fc_els_rscn *head;
140 struct fc_els_rscn_page *page;
Christof Schmitt24073b42008-06-10 18:20:54 +0200141 u16 i;
142 u16 no_entries;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100143 unsigned int afmt;
Christof Schmitt24073b42008-06-10 18:20:54 +0200144
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100145 head = (struct fc_els_rscn *) status_buffer->payload.data;
146 page = (struct fc_els_rscn_page *) head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200147
148 /* see FC-FS */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100149 no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200150
151 for (i = 1; i < no_entries; i++) {
152 /* skip head and start with 1st element */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100153 page++;
154 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
155 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
156 page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200157 }
Swen Schillig9eae07e2009-11-24 16:54:06 +0100158 queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200159}
160
Swen Schillig7ba58c92008-10-01 12:42:18 +0200161static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200162{
Swen Schilligecf0c772009-11-24 16:53:58 +0100163 unsigned long flags;
Christof Schmitt24073b42008-06-10 18:20:54 +0200164 struct zfcp_adapter *adapter = req->adapter;
165 struct zfcp_port *port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200166
Swen Schilligecf0c772009-11-24 16:53:58 +0100167 read_lock_irqsave(&adapter->port_list_lock, flags);
168 list_for_each_entry(port, &adapter->port_list, list)
169 if (port->wwpn == wwpn) {
170 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200171 break;
Swen Schilligecf0c772009-11-24 16:53:58 +0100172 }
173 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200174}
175
176static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
177{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100178 struct fsf_status_read_buffer *status_buffer;
179 struct fc_els_flogi *plogi;
Christof Schmitt24073b42008-06-10 18:20:54 +0200180
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100181 status_buffer = (struct fsf_status_read_buffer *) req->data;
182 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
183 zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200184}
185
186static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
187{
188 struct fsf_status_read_buffer *status_buffer =
189 (struct fsf_status_read_buffer *)req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100190 struct fc_els_logo *logo =
191 (struct fc_els_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200192
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100193 zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200194}
195
196/**
197 * zfcp_fc_incoming_els - handle incoming ELS
198 * @fsf_req - request which contains incoming ELS
199 */
200void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
201{
202 struct fsf_status_read_buffer *status_buffer =
203 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200204 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200205
Swen Schillig57717102009-08-18 15:43:21 +0200206 zfcp_dbf_san_incoming_els(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100207 if (els_type == ELS_PLOGI)
Christof Schmitt24073b42008-06-10 18:20:54 +0200208 zfcp_fc_incoming_plogi(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100209 else if (els_type == ELS_LOGO)
Christof Schmitt24073b42008-06-10 18:20:54 +0200210 zfcp_fc_incoming_logo(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100211 else if (els_type == ELS_RSCN)
Christof Schmitt24073b42008-06-10 18:20:54 +0200212 zfcp_fc_incoming_rscn(fsf_req);
213}
214
Swen Schillig5ab944f2008-10-01 12:42:17 +0200215static void zfcp_fc_ns_handler(unsigned long data)
216{
217 struct zfcp_fc_ns_handler_data *compl_rec =
218 (struct zfcp_fc_ns_handler_data *) data;
219
220 if (compl_rec->handler)
221 compl_rec->handler(compl_rec->handler_data);
222
223 complete(&compl_rec->done);
224}
225
226static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200227{
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100228 struct zfcp_fc_gid_pn *gid_pn = (struct zfcp_fc_gid_pn *) data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200229 struct zfcp_send_ct *ct = &gid_pn->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100230 struct zfcp_fc_gid_pn_req *gid_pn_req = sg_virt(ct->req);
231 struct zfcp_fc_gid_pn_resp *gid_pn_resp = sg_virt(ct->resp);
Christof Schmitt24073b42008-06-10 18:20:54 +0200232 struct zfcp_port *port = gid_pn->port;
233
234 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200235 return;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100236 if (gid_pn_resp->ct_hdr.ct_cmd != FC_FS_ACC)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200237 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100238
Christof Schmitt24073b42008-06-10 18:20:54 +0200239 /* paranoia */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100240 if (gid_pn_req->gid_pn.fn_wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200241 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200242 /* looks like a valid d_id */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100243 port->d_id = ntoh24(gid_pn_resp->gid_pn.fp_fid);
Christof Schmitt24073b42008-06-10 18:20:54 +0200244}
245
Christof Schmitt799b76d2009-08-18 15:43:20 +0200246static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100247 struct zfcp_fc_gid_pn *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200248{
Christof Schmitt799b76d2009-08-18 15:43:20 +0200249 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200250 struct zfcp_fc_ns_handler_data compl_rec;
251 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200252
253 /* setup parameters for send generic command */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200254 gid_pn->port = port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200255 gid_pn->ct.wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200256 gid_pn->ct.handler = zfcp_fc_ns_handler;
257 gid_pn->ct.handler_data = (unsigned long) &compl_rec;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100258 gid_pn->ct.req = &gid_pn->sg_req;
259 gid_pn->ct.resp = &gid_pn->sg_resp;
260 sg_init_one(&gid_pn->sg_req, &gid_pn->gid_pn_req,
261 sizeof(struct zfcp_fc_gid_pn_req));
262 sg_init_one(&gid_pn->sg_resp, &gid_pn->gid_pn_resp,
263 sizeof(struct zfcp_fc_gid_pn_resp));
Christof Schmitt24073b42008-06-10 18:20:54 +0200264
265 /* setup nameserver request */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100266 gid_pn->gid_pn_req.ct_hdr.ct_rev = FC_CT_REV;
267 gid_pn->gid_pn_req.ct_hdr.ct_fs_type = FC_FST_DIR;
268 gid_pn->gid_pn_req.ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
269 gid_pn->gid_pn_req.ct_hdr.ct_options = 0;
270 gid_pn->gid_pn_req.ct_hdr.ct_cmd = FC_NS_GID_PN;
271 gid_pn->gid_pn_req.ct_hdr.ct_mr_size = ZFCP_FC_CT_SIZE_PAGE / 4;
272 gid_pn->gid_pn_req.gid_pn.fn_wwpn = port->wwpn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200273
Swen Schillig5ab944f2008-10-01 12:42:17 +0200274 init_completion(&compl_rec.done);
275 compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
276 compl_rec.handler_data = (unsigned long) gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200277 ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.gid_pn_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200278 if (!ret)
279 wait_for_completion(&compl_rec.done);
280 return ret;
281}
282
283/**
284 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
Christof Schmitt799b76d2009-08-18 15:43:20 +0200285 * @port: port where GID_PN request is needed
Swen Schillig5ab944f2008-10-01 12:42:17 +0200286 * return: -ENOMEM on error, 0 otherwise
287 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200288static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200289{
290 int ret;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100291 struct zfcp_fc_gid_pn *gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200292 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200293
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100294 gid_pn = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200295 if (!gid_pn)
296 return -ENOMEM;
297
298 memset(gid_pn, 0, sizeof(*gid_pn));
299
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200300 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200301 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200302 goto out;
303
Christof Schmitt799b76d2009-08-18 15:43:20 +0200304 ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200305
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200306 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200307out:
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100308 mempool_free(gid_pn, adapter->pool.gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200309 return ret;
310}
311
Christof Schmitt799b76d2009-08-18 15:43:20 +0200312void zfcp_fc_port_did_lookup(struct work_struct *work)
313{
314 int ret;
315 struct zfcp_port *port = container_of(work, struct zfcp_port,
316 gid_pn_work);
317
318 ret = zfcp_fc_ns_gid_pn(port);
319 if (ret) {
320 /* could not issue gid_pn for some reason */
321 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
322 goto out;
323 }
324
325 if (!port->d_id) {
326 zfcp_erp_port_failed(port, "fcgpn_2", NULL);
327 goto out;
328 }
329
330 zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
331out:
Swen Schilligf3450c72009-11-24 16:53:59 +0100332 put_device(&port->sysfs_device);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200333}
334
Christof Schmitt24073b42008-06-10 18:20:54 +0200335/**
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200336 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
337 * @port: The zfcp_port to lookup the d_id for.
338 */
339void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
340{
Swen Schilligf3450c72009-11-24 16:53:59 +0100341 get_device(&port->sysfs_device);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200342 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
Swen Schilligf3450c72009-11-24 16:53:59 +0100343 put_device(&port->sysfs_device);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200344}
345
346/**
Christof Schmitt24073b42008-06-10 18:20:54 +0200347 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
348 * @port: zfcp_port structure
349 * @plogi: plogi payload
350 *
351 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
352 */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100353void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
Christof Schmitt24073b42008-06-10 18:20:54 +0200354{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100355 if (plogi->fl_wwpn != port->wwpn) {
356 port->d_id = 0;
357 dev_warn(&port->adapter->ccw_device->dev,
358 "A port opened with WWPN 0x%016Lx returned data that "
359 "identifies it as WWPN 0x%016Lx\n",
360 (unsigned long long) port->wwpn,
361 (unsigned long long) plogi->fl_wwpn);
362 return;
363 }
364
365 port->wwnn = plogi->fl_wwnn;
366 port->maxframe_size = plogi->fl_csp.sp_bb_data;
367
368 if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200369 port->supported_classes |= FC_COS_CLASS1;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100370 if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200371 port->supported_classes |= FC_COS_CLASS2;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100372 if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200373 port->supported_classes |= FC_COS_CLASS3;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100374 if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200375 port->supported_classes |= FC_COS_CLASS4;
376}
377
Christof Schmitt24073b42008-06-10 18:20:54 +0200378static void zfcp_fc_adisc_handler(unsigned long data)
379{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100380 struct zfcp_fc_els_adisc *adisc = (struct zfcp_fc_els_adisc *) data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200381 struct zfcp_port *port = adisc->els.port;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100382 struct fc_els_adisc *adisc_resp = &adisc->adisc_resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200383
Christof Schmitt3968ce82008-07-02 10:56:32 +0200384 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200385 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200386 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
387 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200388 goto out;
389 }
390
391 if (!port->wwnn)
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100392 port->wwnn = adisc_resp->adisc_wwnn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200393
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100394 if ((port->wwpn != adisc_resp->adisc_wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100395 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100396 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
397 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100398 goto out;
399 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200400
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100401 /* port is good, unblock rport without going through erp */
402 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200403 out:
Christof Schmitt14e242e2009-08-18 15:43:11 +0200404 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Swen Schilligf3450c72009-11-24 16:53:59 +0100405 put_device(&port->sysfs_device);
Christof Schmitt24073b42008-06-10 18:20:54 +0200406 kfree(adisc);
407}
408
409static int zfcp_fc_adisc(struct zfcp_port *port)
410{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100411 struct zfcp_fc_els_adisc *adisc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200412 struct zfcp_adapter *adapter = port->adapter;
413
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100414 adisc = kzalloc(sizeof(struct zfcp_fc_els_adisc), GFP_ATOMIC);
Christof Schmitt24073b42008-06-10 18:20:54 +0200415 if (!adisc)
416 return -ENOMEM;
417
418 adisc->els.req = &adisc->req;
419 adisc->els.resp = &adisc->resp;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100420 sg_init_one(adisc->els.req, &adisc->adisc_req,
421 sizeof(struct fc_els_adisc));
422 sg_init_one(adisc->els.resp, &adisc->adisc_resp,
423 sizeof(struct fc_els_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200424
Christof Schmitt24073b42008-06-10 18:20:54 +0200425 adisc->els.adapter = adapter;
426 adisc->els.port = port;
427 adisc->els.d_id = port->d_id;
428 adisc->els.handler = zfcp_fc_adisc_handler;
429 adisc->els.handler_data = (unsigned long) adisc;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100430 adisc->els.ls_code = adisc->adisc_req.adisc_cmd = ELS_ADISC;
Christof Schmitt24073b42008-06-10 18:20:54 +0200431
432 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
433 without FC-AL-2 capability, so we don't set it */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100434 adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
435 adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
436 hton24(adisc->adisc_req.adisc_port_id,
437 fc_host_port_id(adapter->scsi_host));
Christof Schmitt24073b42008-06-10 18:20:54 +0200438
439 return zfcp_fsf_send_els(&adisc->els);
440}
441
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100442void zfcp_fc_link_test_work(struct work_struct *work)
443{
444 struct zfcp_port *port =
445 container_of(work, struct zfcp_port, test_link_work);
446 int retval;
447
Swen Schilligf3450c72009-11-24 16:53:59 +0100448 get_device(&port->sysfs_device);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100449 port->rport_task = RPORT_DEL;
450 zfcp_scsi_rport_work(&port->rport_work);
451
Christof Schmitt14e242e2009-08-18 15:43:11 +0200452 /* only issue one test command at one time per port */
453 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
454 goto out;
455
456 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
457
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100458 retval = zfcp_fc_adisc(port);
459 if (retval == 0)
460 return;
461
462 /* send of ADISC was not possible */
Christof Schmitt14e242e2009-08-18 15:43:11 +0200463 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100464 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
465
Christof Schmitt14e242e2009-08-18 15:43:11 +0200466out:
Swen Schilligf3450c72009-11-24 16:53:59 +0100467 put_device(&port->sysfs_device);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100468}
469
Christof Schmitt24073b42008-06-10 18:20:54 +0200470/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200471 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200472 * @port: port to be tested
473 *
474 * Test status of a link to a remote port using the ELS command ADISC.
475 * If there is a problem with the remote port, error recovery steps
476 * will be triggered.
477 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200478void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200479{
Swen Schilligf3450c72009-11-24 16:53:59 +0100480 get_device(&port->sysfs_device);
Swen Schillig45446832009-08-18 15:43:17 +0200481 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Swen Schilligf3450c72009-11-24 16:53:59 +0100482 put_device(&port->sysfs_device);
Christof Schmitt24073b42008-06-10 18:20:54 +0200483}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200484
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100485static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200486{
487 struct scatterlist *sg = &gpn_ft->sg_req;
488
Swen Schilliga4623c42009-08-18 15:43:15 +0200489 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100490 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200491
492 kfree(gpn_ft);
493}
494
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100495static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200496{
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100497 struct zfcp_fc_gpn_ft *gpn_ft;
498 struct zfcp_fc_gpn_ft_req *req;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200499
500 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
501 if (!gpn_ft)
502 return NULL;
503
Swen Schilliga4623c42009-08-18 15:43:15 +0200504 req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200505 if (!req) {
506 kfree(gpn_ft);
507 gpn_ft = NULL;
508 goto out;
509 }
510 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
511
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100512 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
513 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200514 gpn_ft = NULL;
515 }
516out:
517 return gpn_ft;
518}
519
520
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100521static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200522 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200523{
524 struct zfcp_send_ct *ct = &gpn_ft->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100525 struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200526 struct zfcp_fc_ns_handler_data compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200527 int ret;
528
529 /* prepare CT IU for GPN_FT */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100530 req->ct_hdr.ct_rev = FC_CT_REV;
531 req->ct_hdr.ct_fs_type = FC_FST_DIR;
532 req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
533 req->ct_hdr.ct_options = 0;
534 req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
535 req->ct_hdr.ct_mr_size = max_bytes / 4;
536 req->gpn_ft.fn_domain_id_scope = 0;
537 req->gpn_ft.fn_area_id_scope = 0;
538 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200539
540 /* prepare zfcp_send_ct */
Sven Schuetz9d544f22009-04-06 18:31:47 +0200541 ct->wka_port = &adapter->gs->ds;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200542 ct->handler = zfcp_fc_ns_handler;
543 ct->handler_data = (unsigned long)&compl_rec;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200544 ct->req = &gpn_ft->sg_req;
545 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200546
Swen Schillig5ab944f2008-10-01 12:42:17 +0200547 init_completion(&compl_rec.done);
548 compl_rec.handler = NULL;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200549 ret = zfcp_fsf_send_ct(ct, NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200550 if (!ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200551 wait_for_completion(&compl_rec.done);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200552 return ret;
553}
554
Swen Schilligf3450c72009-11-24 16:53:59 +0100555static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200556{
Martin Petermann6ab35c02009-04-17 15:08:13 +0200557 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
558 return;
559
Swen Schilligcc8c2822008-06-10 18:21:00 +0200560 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
561
Christof Schmitt04062892008-10-01 12:42:20 +0200562 if ((port->supported_classes != 0) ||
Swen Schilligf3450c72009-11-24 16:53:59 +0100563 !list_empty(&port->unit_list))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200564 return;
Swen Schilligf3450c72009-11-24 16:53:59 +0100565
Swen Schilligf3450c72009-11-24 16:53:59 +0100566 list_move_tail(&port->list, lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200567}
568
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100569static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200570{
571 struct zfcp_send_ct *ct = &gpn_ft->ct;
572 struct scatterlist *sg = gpn_ft->sg_resp;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100573 struct fc_ct_hdr *hdr = sg_virt(sg);
574 struct fc_gpn_ft_resp *acc = sg_virt(sg);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200575 struct zfcp_adapter *adapter = ct->wka_port->adapter;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200576 struct zfcp_port *port, *tmp;
Swen Schilligecf0c772009-11-24 16:53:58 +0100577 unsigned long flags;
Swen Schilligf3450c72009-11-24 16:53:59 +0100578 LIST_HEAD(remove_lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200579 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200580 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200581
582 if (ct->status)
583 return -EIO;
584
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100585 if (hdr->ct_cmd != FC_FS_ACC) {
586 if (hdr->ct_reason == FC_BA_RJT_UNABLE)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200587 return -EAGAIN; /* might be a temporary condition */
588 return -EIO;
589 }
590
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100591 if (hdr->ct_mr_size) {
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100592 dev_warn(&adapter->ccw_device->dev,
593 "The name server reported %d words residual data\n",
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100594 hdr->ct_mr_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200595 return -E2BIG;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100596 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200597
Swen Schilligcc8c2822008-06-10 18:21:00 +0200598 /* first entry is the header */
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100599 for (x = 1; x < max_entries && !last; x++) {
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100600 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200601 acc++;
602 else
603 acc = sg_virt(++sg);
604
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100605 last = acc->fp_flags & FC_NS_FID_LAST;
606 d_id = ntoh24(acc->fp_fid);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200607
Swen Schillig5ab944f2008-10-01 12:42:17 +0200608 /* don't attach ports with a well known address */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100609 if (d_id >= FC_FID_WELL_KNOWN_BASE)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200610 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200611 /* skip the adapter's port and known remote ports */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100612 if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200613 continue;
614
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100615 port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200616 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100617 if (!IS_ERR(port))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100618 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligecf0c772009-11-24 16:53:58 +0100619 else if (PTR_ERR(port) != -EEXIST)
620 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200621 }
622
623 zfcp_erp_wait(adapter);
Swen Schilligecf0c772009-11-24 16:53:58 +0100624 write_lock_irqsave(&adapter->port_list_lock, flags);
625 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100626 zfcp_fc_validate_port(port, &remove_lh);
Swen Schilligecf0c772009-11-24 16:53:58 +0100627 write_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schilligf3450c72009-11-24 16:53:59 +0100628
629 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
630 zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
631 zfcp_device_unregister(&port->sysfs_device,
632 &zfcp_sysfs_port_attrs);
633 }
634
Swen Schilligcc8c2822008-06-10 18:21:00 +0200635 return ret;
636}
637
638/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200639 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schillig9eae07e2009-11-24 16:54:06 +0100640 * @work: reference to scheduled work
Swen Schilligcc8c2822008-06-10 18:21:00 +0200641 */
Swen Schillig9eae07e2009-11-24 16:54:06 +0100642void zfcp_fc_scan_ports(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200643{
Swen Schillig9eae07e2009-11-24 16:54:06 +0100644 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
645 scan_work);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200646 int ret, i;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100647 struct zfcp_fc_gpn_ft *gpn_ft;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100648 int chain, max_entries, buf_num, max_bytes;
649
650 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100651 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
652 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
653 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200654
Swen Schillig306b6ed2009-04-17 15:08:02 +0200655 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
656 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schillig9eae07e2009-11-24 16:54:06 +0100657 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200658
Swen Schillig9eae07e2009-11-24 16:54:06 +0100659 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
660 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200661
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100662 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig9eae07e2009-11-24 16:54:06 +0100663 if (!gpn_ft)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200664 goto out;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200665
666 for (i = 0; i < 3; i++) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200667 ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200668 if (!ret) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200669 ret = zfcp_fc_eval_gpn_ft(gpn_ft, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200670 if (ret == -EAGAIN)
671 ssleep(1);
672 else
673 break;
674 }
675 }
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100676 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200677out:
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200678 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200679}
680
681
Sven Schuetz9d544f22009-04-06 18:31:47 +0200682struct zfcp_els_fc_job {
683 struct zfcp_send_els els;
684 struct fc_bsg_job *job;
685};
686
687static void zfcp_fc_generic_els_handler(unsigned long data)
688{
689 struct zfcp_els_fc_job *els_fc_job = (struct zfcp_els_fc_job *) data;
690 struct fc_bsg_job *job = els_fc_job->job;
691 struct fc_bsg_reply *reply = job->reply;
692
693 if (els_fc_job->els.status) {
694 /* request rejected or timed out */
695 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_REJECT;
696 goto out;
697 }
698
699 reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200700 reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200701
702out:
703 job->state_flags = FC_RQST_STATE_DONE;
704 job->job_done(job);
705 kfree(els_fc_job);
706}
707
708int zfcp_fc_execute_els_fc_job(struct fc_bsg_job *job)
709{
710 struct zfcp_els_fc_job *els_fc_job;
711 struct fc_rport *rport = job->rport;
712 struct Scsi_Host *shost;
713 struct zfcp_adapter *adapter;
714 struct zfcp_port *port;
715 u8 *port_did;
716
717 shost = rport ? rport_to_shost(rport) : job->shost;
718 adapter = (struct zfcp_adapter *)shost->hostdata[0];
719
720 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
721 return -EINVAL;
722
723 els_fc_job = kzalloc(sizeof(struct zfcp_els_fc_job), GFP_KERNEL);
724 if (!els_fc_job)
725 return -ENOMEM;
726
727 els_fc_job->els.adapter = adapter;
728 if (rport) {
Swen Schilligea945ff2009-08-18 15:43:24 +0200729 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200730 if (!port) {
731 kfree(els_fc_job);
732 return -EINVAL;
733 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100734
735 els_fc_job->els.d_id = port->d_id;
Swen Schilligf3450c72009-11-24 16:53:59 +0100736 put_device(&port->sysfs_device);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200737 } else {
738 port_did = job->request->rqst_data.h_els.port_id;
739 els_fc_job->els.d_id = (port_did[0] << 16) +
740 (port_did[1] << 8) + port_did[2];
741 }
742
743 els_fc_job->els.req = job->request_payload.sg_list;
744 els_fc_job->els.resp = job->reply_payload.sg_list;
745 els_fc_job->els.handler = zfcp_fc_generic_els_handler;
746 els_fc_job->els.handler_data = (unsigned long) els_fc_job;
747 els_fc_job->job = job;
748
749 return zfcp_fsf_send_els(&els_fc_job->els);
750}
751
752struct zfcp_ct_fc_job {
753 struct zfcp_send_ct ct;
754 struct fc_bsg_job *job;
755};
756
757static void zfcp_fc_generic_ct_handler(unsigned long data)
758{
759 struct zfcp_ct_fc_job *ct_fc_job = (struct zfcp_ct_fc_job *) data;
760 struct fc_bsg_job *job = ct_fc_job->job;
761
762 job->reply->reply_data.ctels_reply.status = ct_fc_job->ct.status ?
763 FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
Christof Schmittdc577d52009-05-15 13:18:22 +0200764 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200765 job->state_flags = FC_RQST_STATE_DONE;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200766 job->job_done(job);
767
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200768 zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200769
770 kfree(ct_fc_job);
771}
772
773int zfcp_fc_execute_ct_fc_job(struct fc_bsg_job *job)
774{
775 int ret;
776 u8 gs_type;
777 struct fc_rport *rport = job->rport;
778 struct Scsi_Host *shost;
779 struct zfcp_adapter *adapter;
780 struct zfcp_ct_fc_job *ct_fc_job;
781 u32 preamble_word1;
782
783 shost = rport ? rport_to_shost(rport) : job->shost;
784
785 adapter = (struct zfcp_adapter *)shost->hostdata[0];
786 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
787 return -EINVAL;
788
789 ct_fc_job = kzalloc(sizeof(struct zfcp_ct_fc_job), GFP_KERNEL);
790 if (!ct_fc_job)
791 return -ENOMEM;
792
793 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
794 gs_type = (preamble_word1 & 0xff000000) >> 24;
795
796 switch (gs_type) {
797 case FC_FST_ALIAS:
798 ct_fc_job->ct.wka_port = &adapter->gs->as;
799 break;
800 case FC_FST_MGMT:
801 ct_fc_job->ct.wka_port = &adapter->gs->ms;
802 break;
803 case FC_FST_TIME:
804 ct_fc_job->ct.wka_port = &adapter->gs->ts;
805 break;
806 case FC_FST_DIR:
807 ct_fc_job->ct.wka_port = &adapter->gs->ds;
808 break;
809 default:
810 kfree(ct_fc_job);
811 return -EINVAL; /* no such service */
812 }
813
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200814 ret = zfcp_fc_wka_port_get(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200815 if (ret) {
816 kfree(ct_fc_job);
817 return ret;
818 }
819
820 ct_fc_job->ct.req = job->request_payload.sg_list;
821 ct_fc_job->ct.resp = job->reply_payload.sg_list;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200822 ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
823 ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
824 ct_fc_job->ct.completion = NULL;
825 ct_fc_job->job = job;
826
Christof Schmitt799b76d2009-08-18 15:43:20 +0200827 ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200828 if (ret) {
829 kfree(ct_fc_job);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200830 zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200831 }
832 return ret;
833}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200834
835int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
836{
837 struct zfcp_wka_ports *wka_ports;
838
839 wka_ports = kzalloc(sizeof(struct zfcp_wka_ports), GFP_KERNEL);
840 if (!wka_ports)
841 return -ENOMEM;
842
843 adapter->gs = wka_ports;
844 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
845 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
846 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
847 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
848 zfcp_fc_wka_port_init(&wka_ports->ks, FC_FID_SEC_KEY, adapter);
849
850 return 0;
851}
852
853void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
854{
855 kfree(adapter->gs);
856 adapter->gs = NULL;
857}
858