blob: 6d5ccc053e3a61600c1ea192cb14300af36bca86 [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
Christof Schmittbd0072e2009-11-24 16:54:11 +010025static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020026{
27 if (mutex_lock_interruptible(&wka_port->mutex))
28 return -ERESTARTSYS;
29
Christof Schmittbd0072e2009-11-24 16:54:11 +010030 if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
31 wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
32 wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
Swen Schillig5ab944f2008-10-01 12:42:17 +020033 if (zfcp_fsf_open_wka_port(wka_port))
Christof Schmittbd0072e2009-11-24 16:54:11 +010034 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +020035 }
36
37 mutex_unlock(&wka_port->mutex);
38
Swen Schillig27f492c2009-07-13 15:06:13 +020039 wait_event(wka_port->completion_wq,
Christof Schmittbd0072e2009-11-24 16:54:11 +010040 wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
41 wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +020042
Christof Schmittbd0072e2009-11-24 16:54:11 +010043 if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
Swen Schillig5ab944f2008-10-01 12:42:17 +020044 atomic_inc(&wka_port->refcount);
45 return 0;
46 }
47 return -EIO;
48}
49
Swen Schillig6f53a2d2009-08-18 15:43:23 +020050static void zfcp_fc_wka_port_offline(struct work_struct *work)
Swen Schillig5ab944f2008-10-01 12:42:17 +020051{
Jean Delvarebf6aede2009-04-02 16:56:54 -070052 struct delayed_work *dw = to_delayed_work(work);
Christof Schmittbd0072e2009-11-24 16:54:11 +010053 struct zfcp_fc_wka_port *wka_port =
54 container_of(dw, struct zfcp_fc_wka_port, work);
Swen Schillig5ab944f2008-10-01 12:42:17 +020055
Swen Schillig5ab944f2008-10-01 12:42:17 +020056 mutex_lock(&wka_port->mutex);
57 if ((atomic_read(&wka_port->refcount) != 0) ||
Christof Schmittbd0072e2009-11-24 16:54:11 +010058 (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
Swen Schillig5ab944f2008-10-01 12:42:17 +020059 goto out;
60
Christof Schmittbd0072e2009-11-24 16:54:11 +010061 wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
Swen Schillig5ab944f2008-10-01 12:42:17 +020062 if (zfcp_fsf_close_wka_port(wka_port)) {
Christof Schmittbd0072e2009-11-24 16:54:11 +010063 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +020064 wake_up(&wka_port->completion_wq);
65 }
66out:
67 mutex_unlock(&wka_port->mutex);
68}
69
Christof Schmittbd0072e2009-11-24 16:54:11 +010070static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020071{
72 if (atomic_dec_return(&wka_port->refcount) != 0)
73 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +020074 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +020075 schedule_delayed_work(&wka_port->work, HZ / 100);
76}
77
Christof Schmittbd0072e2009-11-24 16:54:11 +010078static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
Sven Schuetz9d544f22009-04-06 18:31:47 +020079 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +020080{
Swen Schillig5ab944f2008-10-01 12:42:17 +020081 init_waitqueue_head(&wka_port->completion_wq);
82
83 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +020084 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +020085
Christof Schmittbd0072e2009-11-24 16:54:11 +010086 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +020087 atomic_set(&wka_port->refcount, 0);
88 mutex_init(&wka_port->mutex);
Swen Schillig6f53a2d2009-08-18 15:43:23 +020089 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
Swen Schillig5ab944f2008-10-01 12:42:17 +020090}
91
Christof Schmittbd0072e2009-11-24 16:54:11 +010092static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
Swen Schillig828bc122009-04-17 15:08:05 +020093{
94 cancel_delayed_work_sync(&wka->work);
95 mutex_lock(&wka->mutex);
Christof Schmittbd0072e2009-11-24 16:54:11 +010096 wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig828bc122009-04-17 15:08:05 +020097 mutex_unlock(&wka->mutex);
98}
99
Christof Schmittbd0072e2009-11-24 16:54:11 +0100100void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
Christof Schmitt55c770f2009-08-18 15:43:12 +0200101{
Swen Schilligf3450c72009-11-24 16:53:59 +0100102 if (!gs)
103 return;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200104 zfcp_fc_wka_port_force_offline(&gs->ms);
105 zfcp_fc_wka_port_force_offline(&gs->ts);
106 zfcp_fc_wka_port_force_offline(&gs->ds);
107 zfcp_fc_wka_port_force_offline(&gs->as);
Christof Schmitt55c770f2009-08-18 15:43:12 +0200108}
109
Christof Schmitt24073b42008-06-10 18:20:54 +0200110static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100111 struct fc_els_rscn_page *page)
Christof Schmitt24073b42008-06-10 18:20:54 +0200112{
113 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +0100114 struct zfcp_adapter *adapter = fsf_req->adapter;
Christof Schmitt24073b42008-06-10 18:20:54 +0200115 struct zfcp_port *port;
116
Swen Schilligecf0c772009-11-24 16:53:58 +0100117 read_lock_irqsave(&adapter->port_list_lock, flags);
118 list_for_each_entry(port, &adapter->port_list, list) {
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100119 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200120 zfcp_fc_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200121 if (!port->d_id)
122 zfcp_erp_port_reopen(port,
123 ZFCP_STATUS_COMMON_ERP_FAILED,
124 "fcrscn1", NULL);
125 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100126 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200127}
128
129static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
130{
131 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100132 struct fc_els_rscn *head;
133 struct fc_els_rscn_page *page;
Christof Schmitt24073b42008-06-10 18:20:54 +0200134 u16 i;
135 u16 no_entries;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100136 unsigned int afmt;
Christof Schmitt24073b42008-06-10 18:20:54 +0200137
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100138 head = (struct fc_els_rscn *) status_buffer->payload.data;
139 page = (struct fc_els_rscn_page *) head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200140
141 /* see FC-FS */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100142 no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200143
144 for (i = 1; i < no_entries; i++) {
145 /* skip head and start with 1st element */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100146 page++;
147 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
148 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
149 page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200150 }
Swen Schillig9eae07e2009-11-24 16:54:06 +0100151 queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200152}
153
Swen Schillig7ba58c92008-10-01 12:42:18 +0200154static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200155{
Swen Schilligecf0c772009-11-24 16:53:58 +0100156 unsigned long flags;
Christof Schmitt24073b42008-06-10 18:20:54 +0200157 struct zfcp_adapter *adapter = req->adapter;
158 struct zfcp_port *port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200159
Swen Schilligecf0c772009-11-24 16:53:58 +0100160 read_lock_irqsave(&adapter->port_list_lock, flags);
161 list_for_each_entry(port, &adapter->port_list, list)
162 if (port->wwpn == wwpn) {
163 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200164 break;
Swen Schilligecf0c772009-11-24 16:53:58 +0100165 }
166 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200167}
168
169static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
170{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100171 struct fsf_status_read_buffer *status_buffer;
172 struct fc_els_flogi *plogi;
Christof Schmitt24073b42008-06-10 18:20:54 +0200173
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100174 status_buffer = (struct fsf_status_read_buffer *) req->data;
175 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
176 zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200177}
178
179static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
180{
181 struct fsf_status_read_buffer *status_buffer =
182 (struct fsf_status_read_buffer *)req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100183 struct fc_els_logo *logo =
184 (struct fc_els_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200185
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100186 zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200187}
188
189/**
190 * zfcp_fc_incoming_els - handle incoming ELS
191 * @fsf_req - request which contains incoming ELS
192 */
193void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
194{
195 struct fsf_status_read_buffer *status_buffer =
196 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200197 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200198
Swen Schillig57717102009-08-18 15:43:21 +0200199 zfcp_dbf_san_incoming_els(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100200 if (els_type == ELS_PLOGI)
Christof Schmitt24073b42008-06-10 18:20:54 +0200201 zfcp_fc_incoming_plogi(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100202 else if (els_type == ELS_LOGO)
Christof Schmitt24073b42008-06-10 18:20:54 +0200203 zfcp_fc_incoming_logo(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100204 else if (els_type == ELS_RSCN)
Christof Schmitt24073b42008-06-10 18:20:54 +0200205 zfcp_fc_incoming_rscn(fsf_req);
206}
207
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100208static void zfcp_fc_ns_gid_pn_eval(void *data)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200209{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100210 struct zfcp_fc_gid_pn *gid_pn = data;
211 struct zfcp_fsf_ct_els *ct = &gid_pn->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100212 struct zfcp_fc_gid_pn_req *gid_pn_req = sg_virt(ct->req);
213 struct zfcp_fc_gid_pn_resp *gid_pn_resp = sg_virt(ct->resp);
Christof Schmitt24073b42008-06-10 18:20:54 +0200214 struct zfcp_port *port = gid_pn->port;
215
216 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200217 return;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100218 if (gid_pn_resp->ct_hdr.ct_cmd != FC_FS_ACC)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200219 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100220
Christof Schmitt24073b42008-06-10 18:20:54 +0200221 /* paranoia */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100222 if (gid_pn_req->gid_pn.fn_wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200223 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200224 /* looks like a valid d_id */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100225 port->d_id = ntoh24(gid_pn_resp->gid_pn.fp_fid);
Christof Schmitt24073b42008-06-10 18:20:54 +0200226}
227
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100228static void zfcp_fc_complete(void *data)
229{
230 complete(data);
231}
232
Christof Schmitt799b76d2009-08-18 15:43:20 +0200233static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100234 struct zfcp_fc_gid_pn *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200235{
Christof Schmitt799b76d2009-08-18 15:43:20 +0200236 struct zfcp_adapter *adapter = port->adapter;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100237 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200238 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200239
240 /* setup parameters for send generic command */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200241 gid_pn->port = port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100242 gid_pn->ct.handler = zfcp_fc_complete;
243 gid_pn->ct.handler_data = &completion;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100244 gid_pn->ct.req = &gid_pn->sg_req;
245 gid_pn->ct.resp = &gid_pn->sg_resp;
246 sg_init_one(&gid_pn->sg_req, &gid_pn->gid_pn_req,
247 sizeof(struct zfcp_fc_gid_pn_req));
248 sg_init_one(&gid_pn->sg_resp, &gid_pn->gid_pn_resp,
249 sizeof(struct zfcp_fc_gid_pn_resp));
Christof Schmitt24073b42008-06-10 18:20:54 +0200250
251 /* setup nameserver request */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100252 gid_pn->gid_pn_req.ct_hdr.ct_rev = FC_CT_REV;
253 gid_pn->gid_pn_req.ct_hdr.ct_fs_type = FC_FST_DIR;
254 gid_pn->gid_pn_req.ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
255 gid_pn->gid_pn_req.ct_hdr.ct_options = 0;
256 gid_pn->gid_pn_req.ct_hdr.ct_cmd = FC_NS_GID_PN;
257 gid_pn->gid_pn_req.ct_hdr.ct_mr_size = ZFCP_FC_CT_SIZE_PAGE / 4;
258 gid_pn->gid_pn_req.gid_pn.fn_wwpn = port->wwpn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200259
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100260 ret = zfcp_fsf_send_ct(&adapter->gs->ds, &gid_pn->ct,
261 adapter->pool.gid_pn_req);
262 if (!ret) {
263 wait_for_completion(&completion);
264 zfcp_fc_ns_gid_pn_eval(gid_pn);
265 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200266 return ret;
267}
268
269/**
270 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
Christof Schmitt799b76d2009-08-18 15:43:20 +0200271 * @port: port where GID_PN request is needed
Swen Schillig5ab944f2008-10-01 12:42:17 +0200272 * return: -ENOMEM on error, 0 otherwise
273 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200274static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200275{
276 int ret;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100277 struct zfcp_fc_gid_pn *gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200278 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200279
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100280 gid_pn = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200281 if (!gid_pn)
282 return -ENOMEM;
283
284 memset(gid_pn, 0, sizeof(*gid_pn));
285
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200286 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200287 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200288 goto out;
289
Christof Schmitt799b76d2009-08-18 15:43:20 +0200290 ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200291
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200292 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200293out:
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100294 mempool_free(gid_pn, adapter->pool.gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200295 return ret;
296}
297
Christof Schmitt799b76d2009-08-18 15:43:20 +0200298void zfcp_fc_port_did_lookup(struct work_struct *work)
299{
300 int ret;
301 struct zfcp_port *port = container_of(work, struct zfcp_port,
302 gid_pn_work);
303
304 ret = zfcp_fc_ns_gid_pn(port);
305 if (ret) {
306 /* could not issue gid_pn for some reason */
307 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
308 goto out;
309 }
310
311 if (!port->d_id) {
312 zfcp_erp_port_failed(port, "fcgpn_2", NULL);
313 goto out;
314 }
315
316 zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
317out:
Swen Schilligf3450c72009-11-24 16:53:59 +0100318 put_device(&port->sysfs_device);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200319}
320
Christof Schmitt24073b42008-06-10 18:20:54 +0200321/**
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200322 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
323 * @port: The zfcp_port to lookup the d_id for.
324 */
325void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
326{
Swen Schilligf3450c72009-11-24 16:53:59 +0100327 get_device(&port->sysfs_device);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200328 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
Swen Schilligf3450c72009-11-24 16:53:59 +0100329 put_device(&port->sysfs_device);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200330}
331
332/**
Christof Schmitt24073b42008-06-10 18:20:54 +0200333 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
334 * @port: zfcp_port structure
335 * @plogi: plogi payload
336 *
337 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
338 */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100339void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
Christof Schmitt24073b42008-06-10 18:20:54 +0200340{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100341 if (plogi->fl_wwpn != port->wwpn) {
342 port->d_id = 0;
343 dev_warn(&port->adapter->ccw_device->dev,
344 "A port opened with WWPN 0x%016Lx returned data that "
345 "identifies it as WWPN 0x%016Lx\n",
346 (unsigned long long) port->wwpn,
347 (unsigned long long) plogi->fl_wwpn);
348 return;
349 }
350
351 port->wwnn = plogi->fl_wwnn;
352 port->maxframe_size = plogi->fl_csp.sp_bb_data;
353
354 if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200355 port->supported_classes |= FC_COS_CLASS1;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100356 if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200357 port->supported_classes |= FC_COS_CLASS2;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100358 if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200359 port->supported_classes |= FC_COS_CLASS3;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100360 if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200361 port->supported_classes |= FC_COS_CLASS4;
362}
363
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100364static void zfcp_fc_adisc_handler(void *data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200365{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100366 struct zfcp_fc_els_adisc *adisc = data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200367 struct zfcp_port *port = adisc->els.port;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100368 struct fc_els_adisc *adisc_resp = &adisc->adisc_resp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200369
Christof Schmitt3968ce82008-07-02 10:56:32 +0200370 if (adisc->els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200371 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200372 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
373 "fcadh_1", NULL);
Christof Schmitt24073b42008-06-10 18:20:54 +0200374 goto out;
375 }
376
377 if (!port->wwnn)
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100378 port->wwnn = adisc_resp->adisc_wwnn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200379
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100380 if ((port->wwpn != adisc_resp->adisc_wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100381 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100382 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
383 "fcadh_2", NULL);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100384 goto out;
385 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200386
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100387 /* port is good, unblock rport without going through erp */
388 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200389 out:
Christof Schmitt14e242e2009-08-18 15:43:11 +0200390 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Swen Schilligf3450c72009-11-24 16:53:59 +0100391 put_device(&port->sysfs_device);
Christof Schmitt24073b42008-06-10 18:20:54 +0200392 kfree(adisc);
393}
394
395static int zfcp_fc_adisc(struct zfcp_port *port)
396{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100397 struct zfcp_fc_els_adisc *adisc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200398 struct zfcp_adapter *adapter = port->adapter;
399
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100400 adisc = kzalloc(sizeof(struct zfcp_fc_els_adisc), GFP_ATOMIC);
Christof Schmitt24073b42008-06-10 18:20:54 +0200401 if (!adisc)
402 return -ENOMEM;
403
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100404 adisc->els.port = port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200405 adisc->els.req = &adisc->req;
406 adisc->els.resp = &adisc->resp;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100407 sg_init_one(adisc->els.req, &adisc->adisc_req,
408 sizeof(struct fc_els_adisc));
409 sg_init_one(adisc->els.resp, &adisc->adisc_resp,
410 sizeof(struct fc_els_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200411
Christof Schmitt24073b42008-06-10 18:20:54 +0200412 adisc->els.handler = zfcp_fc_adisc_handler;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100413 adisc->els.handler_data = adisc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200414
415 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
416 without FC-AL-2 capability, so we don't set it */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100417 adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
418 adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100419 adisc->adisc_req.adisc_cmd = ELS_ADISC;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100420 hton24(adisc->adisc_req.adisc_port_id,
421 fc_host_port_id(adapter->scsi_host));
Christof Schmitt24073b42008-06-10 18:20:54 +0200422
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100423 return zfcp_fsf_send_els(adapter, port->d_id, &adisc->els);
Christof Schmitt24073b42008-06-10 18:20:54 +0200424}
425
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100426void zfcp_fc_link_test_work(struct work_struct *work)
427{
428 struct zfcp_port *port =
429 container_of(work, struct zfcp_port, test_link_work);
430 int retval;
431
Swen Schilligf3450c72009-11-24 16:53:59 +0100432 get_device(&port->sysfs_device);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100433 port->rport_task = RPORT_DEL;
434 zfcp_scsi_rport_work(&port->rport_work);
435
Christof Schmitt14e242e2009-08-18 15:43:11 +0200436 /* only issue one test command at one time per port */
437 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
438 goto out;
439
440 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
441
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100442 retval = zfcp_fc_adisc(port);
443 if (retval == 0)
444 return;
445
446 /* send of ADISC was not possible */
Christof Schmitt14e242e2009-08-18 15:43:11 +0200447 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100448 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
449
Christof Schmitt14e242e2009-08-18 15:43:11 +0200450out:
Swen Schilligf3450c72009-11-24 16:53:59 +0100451 put_device(&port->sysfs_device);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100452}
453
Christof Schmitt24073b42008-06-10 18:20:54 +0200454/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200455 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200456 * @port: port to be tested
457 *
458 * Test status of a link to a remote port using the ELS command ADISC.
459 * If there is a problem with the remote port, error recovery steps
460 * will be triggered.
461 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200462void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200463{
Swen Schilligf3450c72009-11-24 16:53:59 +0100464 get_device(&port->sysfs_device);
Swen Schillig45446832009-08-18 15:43:17 +0200465 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Swen Schilligf3450c72009-11-24 16:53:59 +0100466 put_device(&port->sysfs_device);
Christof Schmitt24073b42008-06-10 18:20:54 +0200467}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200468
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100469static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200470{
471 struct scatterlist *sg = &gpn_ft->sg_req;
472
Swen Schilliga4623c42009-08-18 15:43:15 +0200473 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100474 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200475
476 kfree(gpn_ft);
477}
478
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100479static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200480{
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100481 struct zfcp_fc_gpn_ft *gpn_ft;
482 struct zfcp_fc_gpn_ft_req *req;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200483
484 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
485 if (!gpn_ft)
486 return NULL;
487
Swen Schilliga4623c42009-08-18 15:43:15 +0200488 req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200489 if (!req) {
490 kfree(gpn_ft);
491 gpn_ft = NULL;
492 goto out;
493 }
494 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
495
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100496 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
497 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200498 gpn_ft = NULL;
499 }
500out:
501 return gpn_ft;
502}
503
504
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100505static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200506 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200507{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100508 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100509 struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100510 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200511 int ret;
512
513 /* prepare CT IU for GPN_FT */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100514 req->ct_hdr.ct_rev = FC_CT_REV;
515 req->ct_hdr.ct_fs_type = FC_FST_DIR;
516 req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
517 req->ct_hdr.ct_options = 0;
518 req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
519 req->ct_hdr.ct_mr_size = max_bytes / 4;
520 req->gpn_ft.fn_domain_id_scope = 0;
521 req->gpn_ft.fn_area_id_scope = 0;
522 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200523
524 /* prepare zfcp_send_ct */
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100525 ct->handler = zfcp_fc_complete;
526 ct->handler_data = &completion;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200527 ct->req = &gpn_ft->sg_req;
528 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200529
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100530 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct, NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200531 if (!ret)
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100532 wait_for_completion(&completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200533 return ret;
534}
535
Swen Schilligf3450c72009-11-24 16:53:59 +0100536static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200537{
Martin Petermann6ab35c02009-04-17 15:08:13 +0200538 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
539 return;
540
Swen Schilligcc8c2822008-06-10 18:21:00 +0200541 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
542
Christof Schmitt04062892008-10-01 12:42:20 +0200543 if ((port->supported_classes != 0) ||
Swen Schilligf3450c72009-11-24 16:53:59 +0100544 !list_empty(&port->unit_list))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200545 return;
Swen Schilligf3450c72009-11-24 16:53:59 +0100546
Swen Schilligf3450c72009-11-24 16:53:59 +0100547 list_move_tail(&port->list, lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200548}
549
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100550static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
551 struct zfcp_adapter *adapter, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200552{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100553 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200554 struct scatterlist *sg = gpn_ft->sg_resp;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100555 struct fc_ct_hdr *hdr = sg_virt(sg);
556 struct fc_gpn_ft_resp *acc = sg_virt(sg);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200557 struct zfcp_port *port, *tmp;
Swen Schilligecf0c772009-11-24 16:53:58 +0100558 unsigned long flags;
Swen Schilligf3450c72009-11-24 16:53:59 +0100559 LIST_HEAD(remove_lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200560 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200561 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200562
563 if (ct->status)
564 return -EIO;
565
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100566 if (hdr->ct_cmd != FC_FS_ACC) {
567 if (hdr->ct_reason == FC_BA_RJT_UNABLE)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200568 return -EAGAIN; /* might be a temporary condition */
569 return -EIO;
570 }
571
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100572 if (hdr->ct_mr_size) {
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100573 dev_warn(&adapter->ccw_device->dev,
574 "The name server reported %d words residual data\n",
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100575 hdr->ct_mr_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200576 return -E2BIG;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100577 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200578
Swen Schilligcc8c2822008-06-10 18:21:00 +0200579 /* first entry is the header */
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100580 for (x = 1; x < max_entries && !last; x++) {
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100581 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200582 acc++;
583 else
584 acc = sg_virt(++sg);
585
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100586 last = acc->fp_flags & FC_NS_FID_LAST;
587 d_id = ntoh24(acc->fp_fid);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200588
Swen Schillig5ab944f2008-10-01 12:42:17 +0200589 /* don't attach ports with a well known address */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100590 if (d_id >= FC_FID_WELL_KNOWN_BASE)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200591 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200592 /* skip the adapter's port and known remote ports */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100593 if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200594 continue;
595
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100596 port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200597 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100598 if (!IS_ERR(port))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100599 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligecf0c772009-11-24 16:53:58 +0100600 else if (PTR_ERR(port) != -EEXIST)
601 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200602 }
603
604 zfcp_erp_wait(adapter);
Swen Schilligecf0c772009-11-24 16:53:58 +0100605 write_lock_irqsave(&adapter->port_list_lock, flags);
606 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100607 zfcp_fc_validate_port(port, &remove_lh);
Swen Schilligecf0c772009-11-24 16:53:58 +0100608 write_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schilligf3450c72009-11-24 16:53:59 +0100609
610 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
611 zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
612 zfcp_device_unregister(&port->sysfs_device,
613 &zfcp_sysfs_port_attrs);
614 }
615
Swen Schilligcc8c2822008-06-10 18:21:00 +0200616 return ret;
617}
618
619/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200620 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schillig9eae07e2009-11-24 16:54:06 +0100621 * @work: reference to scheduled work
Swen Schilligcc8c2822008-06-10 18:21:00 +0200622 */
Swen Schillig9eae07e2009-11-24 16:54:06 +0100623void zfcp_fc_scan_ports(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200624{
Swen Schillig9eae07e2009-11-24 16:54:06 +0100625 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
626 scan_work);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200627 int ret, i;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100628 struct zfcp_fc_gpn_ft *gpn_ft;
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100629 int chain, max_entries, buf_num, max_bytes;
630
631 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100632 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
633 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
634 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200635
Swen Schillig306b6ed2009-04-17 15:08:02 +0200636 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
637 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schillig9eae07e2009-11-24 16:54:06 +0100638 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200639
Swen Schillig9eae07e2009-11-24 16:54:06 +0100640 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
641 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200642
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100643 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig9eae07e2009-11-24 16:54:06 +0100644 if (!gpn_ft)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200645 goto out;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200646
647 for (i = 0; i < 3; i++) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200648 ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200649 if (!ret) {
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100650 ret = zfcp_fc_eval_gpn_ft(gpn_ft, adapter, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200651 if (ret == -EAGAIN)
652 ssleep(1);
653 else
654 break;
655 }
656 }
Christof Schmitt39eb7e92008-12-19 16:57:01 +0100657 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200658out:
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200659 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200660}
661
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100662static void zfcp_fc_ct_els_job_handler(void *data)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200663{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100664 struct fc_bsg_job *job = data;
665 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
666 int status = zfcp_ct_els->status;
667 int reply_status;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200668
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100669 reply_status = status ? FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
670 job->reply->reply_data.ctels_reply.status = reply_status;
671 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200672 job->job_done(job);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200673}
674
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100675static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
676 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200677{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100678 struct zfcp_fsf_ct_els *els = job->dd_data;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200679 struct fc_rport *rport = job->rport;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200680 struct zfcp_port *port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100681 u32 d_id;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200682
Sven Schuetz9d544f22009-04-06 18:31:47 +0200683 if (rport) {
Swen Schilligea945ff2009-08-18 15:43:24 +0200684 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100685 if (!port)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200686 return -EINVAL;
Swen Schilligecf0c772009-11-24 16:53:58 +0100687
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100688 d_id = port->d_id;
Swen Schilligf3450c72009-11-24 16:53:59 +0100689 put_device(&port->sysfs_device);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100690 } else
691 d_id = ntoh24(job->request->rqst_data.h_els.port_id);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200692
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100693 return zfcp_fsf_send_els(adapter, d_id, els);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200694}
695
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100696static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
697 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200698{
699 int ret;
700 u8 gs_type;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100701 struct zfcp_fsf_ct_els *ct = job->dd_data;
702 struct zfcp_fc_wka_port *wka_port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200703 u32 preamble_word1;
704
Sven Schuetz9d544f22009-04-06 18:31:47 +0200705 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
706 gs_type = (preamble_word1 & 0xff000000) >> 24;
707
708 switch (gs_type) {
709 case FC_FST_ALIAS:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100710 wka_port = &adapter->gs->as;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200711 break;
712 case FC_FST_MGMT:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100713 wka_port = &adapter->gs->ms;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200714 break;
715 case FC_FST_TIME:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100716 wka_port = &adapter->gs->ts;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200717 break;
718 case FC_FST_DIR:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100719 wka_port = &adapter->gs->ds;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200720 break;
721 default:
Sven Schuetz9d544f22009-04-06 18:31:47 +0200722 return -EINVAL; /* no such service */
723 }
724
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100725 ret = zfcp_fc_wka_port_get(wka_port);
726 if (ret)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200727 return ret;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200728
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100729 ret = zfcp_fsf_send_ct(wka_port, ct, NULL);
730 if (ret)
731 zfcp_fc_wka_port_put(wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200732
Sven Schuetz9d544f22009-04-06 18:31:47 +0200733 return ret;
734}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200735
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100736int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
737{
738 struct Scsi_Host *shost;
739 struct zfcp_adapter *adapter;
740 struct zfcp_fsf_ct_els *ct_els = job->dd_data;
741
742 shost = job->rport ? rport_to_shost(job->rport) : job->shost;
743 adapter = (struct zfcp_adapter *)shost->hostdata[0];
744
745 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
746 return -EINVAL;
747
748 ct_els->req = job->request_payload.sg_list;
749 ct_els->resp = job->reply_payload.sg_list;
750 ct_els->handler = zfcp_fc_ct_els_job_handler;
751 ct_els->handler_data = job;
752
753 switch (job->request->msgcode) {
754 case FC_BSG_RPT_ELS:
755 case FC_BSG_HST_ELS_NOLOGIN:
756 return zfcp_fc_exec_els_job(job, adapter);
757 case FC_BSG_RPT_CT:
758 case FC_BSG_HST_CT:
759 return zfcp_fc_exec_ct_job(job, adapter);
760 default:
761 return -EINVAL;
762 }
763}
764
Swen Schilligd5a282a2009-08-18 15:43:22 +0200765int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
766{
Christof Schmittbd0072e2009-11-24 16:54:11 +0100767 struct zfcp_fc_wka_ports *wka_ports;
Swen Schilligd5a282a2009-08-18 15:43:22 +0200768
Christof Schmittbd0072e2009-11-24 16:54:11 +0100769 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200770 if (!wka_ports)
771 return -ENOMEM;
772
773 adapter->gs = wka_ports;
774 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
775 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
776 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
777 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200778
779 return 0;
780}
781
782void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
783{
784 kfree(adapter->gs);
785 adapter->gs = NULL;
786}
787