blob: ac5e3b7a3576d85103a40f980c381ec5ee2ebfeb [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 Schmittee744622009-11-24 16:54:14 +0100392 kmem_cache_free(zfcp_data.adisc_cache, adisc);
Christof Schmitt24073b42008-06-10 18:20:54 +0200393}
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;
Christof Schmittee744622009-11-24 16:54:14 +0100399 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200400
Christof Schmittee744622009-11-24 16:54:14 +0100401 adisc = kmem_cache_alloc(zfcp_data.adisc_cache, GFP_ATOMIC);
Christof Schmitt24073b42008-06-10 18:20:54 +0200402 if (!adisc)
403 return -ENOMEM;
404
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100405 adisc->els.port = port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200406 adisc->els.req = &adisc->req;
407 adisc->els.resp = &adisc->resp;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100408 sg_init_one(adisc->els.req, &adisc->adisc_req,
409 sizeof(struct fc_els_adisc));
410 sg_init_one(adisc->els.resp, &adisc->adisc_resp,
411 sizeof(struct fc_els_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200412
Christof Schmitt24073b42008-06-10 18:20:54 +0200413 adisc->els.handler = zfcp_fc_adisc_handler;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100414 adisc->els.handler_data = adisc;
Christof Schmitt24073b42008-06-10 18:20:54 +0200415
416 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
417 without FC-AL-2 capability, so we don't set it */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100418 adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
419 adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100420 adisc->adisc_req.adisc_cmd = ELS_ADISC;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100421 hton24(adisc->adisc_req.adisc_port_id,
422 fc_host_port_id(adapter->scsi_host));
Christof Schmitt24073b42008-06-10 18:20:54 +0200423
Christof Schmittee744622009-11-24 16:54:14 +0100424 ret = zfcp_fsf_send_els(adapter, port->d_id, &adisc->els);
425 if (ret)
426 kmem_cache_free(zfcp_data.adisc_cache, adisc);
427
428 return ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200429}
430
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100431void zfcp_fc_link_test_work(struct work_struct *work)
432{
433 struct zfcp_port *port =
434 container_of(work, struct zfcp_port, test_link_work);
435 int retval;
436
Swen Schilligf3450c72009-11-24 16:53:59 +0100437 get_device(&port->sysfs_device);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100438 port->rport_task = RPORT_DEL;
439 zfcp_scsi_rport_work(&port->rport_work);
440
Christof Schmitt14e242e2009-08-18 15:43:11 +0200441 /* only issue one test command at one time per port */
442 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
443 goto out;
444
445 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
446
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100447 retval = zfcp_fc_adisc(port);
448 if (retval == 0)
449 return;
450
451 /* send of ADISC was not possible */
Christof Schmitt14e242e2009-08-18 15:43:11 +0200452 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100453 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
454
Christof Schmitt14e242e2009-08-18 15:43:11 +0200455out:
Swen Schilligf3450c72009-11-24 16:53:59 +0100456 put_device(&port->sysfs_device);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100457}
458
Christof Schmitt24073b42008-06-10 18:20:54 +0200459/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200460 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200461 * @port: port to be tested
462 *
463 * Test status of a link to a remote port using the ELS command ADISC.
464 * If there is a problem with the remote port, error recovery steps
465 * will be triggered.
466 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200467void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200468{
Swen Schilligf3450c72009-11-24 16:53:59 +0100469 get_device(&port->sysfs_device);
Swen Schillig45446832009-08-18 15:43:17 +0200470 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Swen Schilligf3450c72009-11-24 16:53:59 +0100471 put_device(&port->sysfs_device);
Christof Schmitt24073b42008-06-10 18:20:54 +0200472}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200473
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100474static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200475{
476 struct scatterlist *sg = &gpn_ft->sg_req;
477
Swen Schilliga4623c42009-08-18 15:43:15 +0200478 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100479 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200480
481 kfree(gpn_ft);
482}
483
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100484static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200485{
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100486 struct zfcp_fc_gpn_ft *gpn_ft;
487 struct zfcp_fc_gpn_ft_req *req;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200488
489 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
490 if (!gpn_ft)
491 return NULL;
492
Swen Schilliga4623c42009-08-18 15:43:15 +0200493 req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200494 if (!req) {
495 kfree(gpn_ft);
496 gpn_ft = NULL;
497 goto out;
498 }
499 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
500
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100501 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
502 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200503 gpn_ft = NULL;
504 }
505out:
506 return gpn_ft;
507}
508
509
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100510static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200511 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200512{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100513 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100514 struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100515 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200516 int ret;
517
518 /* prepare CT IU for GPN_FT */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100519 req->ct_hdr.ct_rev = FC_CT_REV;
520 req->ct_hdr.ct_fs_type = FC_FST_DIR;
521 req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
522 req->ct_hdr.ct_options = 0;
523 req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
524 req->ct_hdr.ct_mr_size = max_bytes / 4;
525 req->gpn_ft.fn_domain_id_scope = 0;
526 req->gpn_ft.fn_area_id_scope = 0;
527 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200528
529 /* prepare zfcp_send_ct */
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100530 ct->handler = zfcp_fc_complete;
531 ct->handler_data = &completion;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200532 ct->req = &gpn_ft->sg_req;
533 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200534
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100535 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct, NULL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200536 if (!ret)
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100537 wait_for_completion(&completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200538 return ret;
539}
540
Swen Schilligf3450c72009-11-24 16:53:59 +0100541static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200542{
Martin Petermann6ab35c02009-04-17 15:08:13 +0200543 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
544 return;
545
Swen Schilligcc8c2822008-06-10 18:21:00 +0200546 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
547
Christof Schmitt04062892008-10-01 12:42:20 +0200548 if ((port->supported_classes != 0) ||
Swen Schilligf3450c72009-11-24 16:53:59 +0100549 !list_empty(&port->unit_list))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200550 return;
Swen Schilligf3450c72009-11-24 16:53:59 +0100551
Swen Schilligf3450c72009-11-24 16:53:59 +0100552 list_move_tail(&port->list, lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200553}
554
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100555static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
556 struct zfcp_adapter *adapter, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200557{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100558 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200559 struct scatterlist *sg = gpn_ft->sg_resp;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100560 struct fc_ct_hdr *hdr = sg_virt(sg);
561 struct fc_gpn_ft_resp *acc = sg_virt(sg);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200562 struct zfcp_port *port, *tmp;
Swen Schilligecf0c772009-11-24 16:53:58 +0100563 unsigned long flags;
Swen Schilligf3450c72009-11-24 16:53:59 +0100564 LIST_HEAD(remove_lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200565 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200566 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200567
568 if (ct->status)
569 return -EIO;
570
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100571 if (hdr->ct_cmd != FC_FS_ACC) {
572 if (hdr->ct_reason == FC_BA_RJT_UNABLE)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200573 return -EAGAIN; /* might be a temporary condition */
574 return -EIO;
575 }
576
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100577 if (hdr->ct_mr_size) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100578 dev_warn(&adapter->ccw_device->dev,
579 "The name server reported %d words residual data\n",
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100580 hdr->ct_mr_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200581 return -E2BIG;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100582 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200583
Swen Schilligcc8c2822008-06-10 18:21:00 +0200584 /* first entry is the header */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100585 for (x = 1; x < max_entries && !last; x++) {
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100586 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200587 acc++;
588 else
589 acc = sg_virt(++sg);
590
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100591 last = acc->fp_flags & FC_NS_FID_LAST;
592 d_id = ntoh24(acc->fp_fid);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200593
Swen Schillig5ab944f2008-10-01 12:42:17 +0200594 /* don't attach ports with a well known address */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100595 if (d_id >= FC_FID_WELL_KNOWN_BASE)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200596 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200597 /* skip the adapter's port and known remote ports */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100598 if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200599 continue;
600
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100601 port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200602 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100603 if (!IS_ERR(port))
Swen Schillig5ffd51a2009-03-02 13:09:04 +0100604 zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
Swen Schilligecf0c772009-11-24 16:53:58 +0100605 else if (PTR_ERR(port) != -EEXIST)
606 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200607 }
608
609 zfcp_erp_wait(adapter);
Swen Schilligecf0c772009-11-24 16:53:58 +0100610 write_lock_irqsave(&adapter->port_list_lock, flags);
611 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100612 zfcp_fc_validate_port(port, &remove_lh);
Swen Schilligecf0c772009-11-24 16:53:58 +0100613 write_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schilligf3450c72009-11-24 16:53:59 +0100614
615 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
616 zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
617 zfcp_device_unregister(&port->sysfs_device,
618 &zfcp_sysfs_port_attrs);
619 }
620
Swen Schilligcc8c2822008-06-10 18:21:00 +0200621 return ret;
622}
623
624/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200625 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schillig9eae07e2009-11-24 16:54:06 +0100626 * @work: reference to scheduled work
Swen Schilligcc8c2822008-06-10 18:21:00 +0200627 */
Swen Schillig9eae07e2009-11-24 16:54:06 +0100628void zfcp_fc_scan_ports(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200629{
Swen Schillig9eae07e2009-11-24 16:54:06 +0100630 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
631 scan_work);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200632 int ret, i;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100633 struct zfcp_fc_gpn_ft *gpn_ft;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100634 int chain, max_entries, buf_num, max_bytes;
635
636 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100637 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
638 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
639 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200640
Swen Schillig306b6ed2009-04-17 15:08:02 +0200641 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
642 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schillig9eae07e2009-11-24 16:54:06 +0100643 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200644
Swen Schillig9eae07e2009-11-24 16:54:06 +0100645 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
646 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200647
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100648 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig9eae07e2009-11-24 16:54:06 +0100649 if (!gpn_ft)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200650 goto out;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200651
652 for (i = 0; i < 3; i++) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200653 ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200654 if (!ret) {
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100655 ret = zfcp_fc_eval_gpn_ft(gpn_ft, adapter, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200656 if (ret == -EAGAIN)
657 ssleep(1);
658 else
659 break;
660 }
661 }
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100662 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200663out:
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200664 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200665}
666
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100667static void zfcp_fc_ct_els_job_handler(void *data)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200668{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100669 struct fc_bsg_job *job = data;
670 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
671 int status = zfcp_ct_els->status;
672 int reply_status;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200673
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100674 reply_status = status ? FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
675 job->reply->reply_data.ctels_reply.status = reply_status;
676 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200677 job->job_done(job);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200678}
679
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100680static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
681 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200682{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100683 struct zfcp_fsf_ct_els *els = job->dd_data;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200684 struct fc_rport *rport = job->rport;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200685 struct zfcp_port *port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100686 u32 d_id;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200687
Sven Schuetz9d544f22009-04-06 18:31:47 +0200688 if (rport) {
Swen Schilligea945ff2009-08-18 15:43:24 +0200689 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100690 if (!port)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200691 return -EINVAL;
Swen Schilligecf0c772009-11-24 16:53:58 +0100692
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100693 d_id = port->d_id;
Swen Schilligf3450c72009-11-24 16:53:59 +0100694 put_device(&port->sysfs_device);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100695 } else
696 d_id = ntoh24(job->request->rqst_data.h_els.port_id);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200697
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100698 return zfcp_fsf_send_els(adapter, d_id, els);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200699}
700
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100701static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
702 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200703{
704 int ret;
705 u8 gs_type;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100706 struct zfcp_fsf_ct_els *ct = job->dd_data;
707 struct zfcp_fc_wka_port *wka_port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200708 u32 preamble_word1;
709
Sven Schuetz9d544f22009-04-06 18:31:47 +0200710 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
711 gs_type = (preamble_word1 & 0xff000000) >> 24;
712
713 switch (gs_type) {
714 case FC_FST_ALIAS:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100715 wka_port = &adapter->gs->as;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200716 break;
717 case FC_FST_MGMT:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100718 wka_port = &adapter->gs->ms;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200719 break;
720 case FC_FST_TIME:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100721 wka_port = &adapter->gs->ts;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200722 break;
723 case FC_FST_DIR:
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100724 wka_port = &adapter->gs->ds;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200725 break;
726 default:
Sven Schuetz9d544f22009-04-06 18:31:47 +0200727 return -EINVAL; /* no such service */
728 }
729
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100730 ret = zfcp_fc_wka_port_get(wka_port);
731 if (ret)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200732 return ret;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200733
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100734 ret = zfcp_fsf_send_ct(wka_port, ct, NULL);
735 if (ret)
736 zfcp_fc_wka_port_put(wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200737
Sven Schuetz9d544f22009-04-06 18:31:47 +0200738 return ret;
739}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200740
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100741int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
742{
743 struct Scsi_Host *shost;
744 struct zfcp_adapter *adapter;
745 struct zfcp_fsf_ct_els *ct_els = job->dd_data;
746
747 shost = job->rport ? rport_to_shost(job->rport) : job->shost;
748 adapter = (struct zfcp_adapter *)shost->hostdata[0];
749
750 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
751 return -EINVAL;
752
753 ct_els->req = job->request_payload.sg_list;
754 ct_els->resp = job->reply_payload.sg_list;
755 ct_els->handler = zfcp_fc_ct_els_job_handler;
756 ct_els->handler_data = job;
757
758 switch (job->request->msgcode) {
759 case FC_BSG_RPT_ELS:
760 case FC_BSG_HST_ELS_NOLOGIN:
761 return zfcp_fc_exec_els_job(job, adapter);
762 case FC_BSG_RPT_CT:
763 case FC_BSG_HST_CT:
764 return zfcp_fc_exec_ct_job(job, adapter);
765 default:
766 return -EINVAL;
767 }
768}
769
Swen Schilligd5a282a2009-08-18 15:43:22 +0200770int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
771{
Christof Schmittbd0072e2009-11-24 16:54:11 +0100772 struct zfcp_fc_wka_ports *wka_ports;
Swen Schilligd5a282a2009-08-18 15:43:22 +0200773
Christof Schmittbd0072e2009-11-24 16:54:11 +0100774 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200775 if (!wka_ports)
776 return -ENOMEM;
777
778 adapter->gs = wka_ports;
779 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
780 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
781 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
782 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200783
784 return 0;
785}
786
787void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
788{
789 kfree(adapter->gs);
790 adapter->gs = NULL;
791}
792