blob: e020dec8529431d150ec961b56a2d4893156fdbd [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 Schmitt615f59e2010-02-17 11:18:56 +01006 * Copyright IBM Corporation 2008, 2010
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Christof Schmitt9d05ce22009-11-24 16:54:09 +010014#include <scsi/fc/fc_els.h>
15#include <scsi/libfc.h>
Christof Schmitt24073b42008-06-10 18:20:54 +020016#include "zfcp_ext.h"
Christof Schmitt9d05ce22009-11-24 16:54:09 +010017#include "zfcp_fc.h"
Christof Schmitt24073b42008-06-10 18:20:54 +020018
Christof Schmitt087897e2011-02-22 19:54:41 +010019struct kmem_cache *zfcp_fc_req_cache;
20
Christof Schmitt9d05ce22009-11-24 16:54:09 +010021static u32 zfcp_fc_rscn_range_mask[] = {
22 [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
23 [ELS_ADDR_FMT_AREA] = 0xFFFF00,
24 [ELS_ADDR_FMT_DOM] = 0xFF0000,
25 [ELS_ADDR_FMT_FAB] = 0x000000,
Christof Schmitte0d7fcb2008-12-19 16:56:58 +010026};
27
Sven Schuetz2d1e5472010-07-16 15:37:39 +020028/**
29 * zfcp_fc_post_event - post event to userspace via fc_transport
30 * @work: work struct with enqueued events
31 */
32void zfcp_fc_post_event(struct work_struct *work)
33{
34 struct zfcp_fc_event *event = NULL, *tmp = NULL;
35 LIST_HEAD(tmp_lh);
36 struct zfcp_fc_events *events = container_of(work,
37 struct zfcp_fc_events, work);
38 struct zfcp_adapter *adapter = container_of(events, struct zfcp_adapter,
39 events);
40
41 spin_lock_bh(&events->list_lock);
42 list_splice_init(&events->list, &tmp_lh);
43 spin_unlock_bh(&events->list_lock);
44
45 list_for_each_entry_safe(event, tmp, &tmp_lh, list) {
46 fc_host_post_event(adapter->scsi_host, fc_get_event_number(),
47 event->code, event->data);
48 list_del(&event->list);
49 kfree(event);
50 }
51
52}
53
54/**
55 * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context
56 * @adapter: The adapter where to enqueue the event
57 * @event_code: The event code (as defined in fc_host_event_code in
58 * scsi_transport_fc.h)
59 * @event_data: The event data (e.g. n_port page in case of els)
60 */
61void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
62 enum fc_host_event_code event_code, u32 event_data)
63{
64 struct zfcp_fc_event *event;
65
66 event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
67 if (!event)
68 return;
69
70 event->code = event_code;
71 event->data = event_data;
72
73 spin_lock(&adapter->events.list_lock);
74 list_add_tail(&event->list, &adapter->events.list);
75 spin_unlock(&adapter->events.list_lock);
76
77 queue_work(adapter->work_queue, &adapter->events.work);
78}
79
Christof Schmittbd0072e2009-11-24 16:54:11 +010080static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +020081{
82 if (mutex_lock_interruptible(&wka_port->mutex))
83 return -ERESTARTSYS;
84
Christof Schmittbd0072e2009-11-24 16:54:11 +010085 if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
86 wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
87 wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
Swen Schillig5ab944f2008-10-01 12:42:17 +020088 if (zfcp_fsf_open_wka_port(wka_port))
Christof Schmittbd0072e2009-11-24 16:54:11 +010089 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +020090 }
91
92 mutex_unlock(&wka_port->mutex);
93
Swen Schillig27f492c2009-07-13 15:06:13 +020094 wait_event(wka_port->completion_wq,
Christof Schmittbd0072e2009-11-24 16:54:11 +010095 wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
96 wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
Swen Schillig5ab944f2008-10-01 12:42:17 +020097
Christof Schmittbd0072e2009-11-24 16:54:11 +010098 if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
Swen Schillig5ab944f2008-10-01 12:42:17 +020099 atomic_inc(&wka_port->refcount);
100 return 0;
101 }
102 return -EIO;
103}
104
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200105static void zfcp_fc_wka_port_offline(struct work_struct *work)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200106{
Jean Delvarebf6aede2009-04-02 16:56:54 -0700107 struct delayed_work *dw = to_delayed_work(work);
Christof Schmittbd0072e2009-11-24 16:54:11 +0100108 struct zfcp_fc_wka_port *wka_port =
109 container_of(dw, struct zfcp_fc_wka_port, work);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200110
Swen Schillig5ab944f2008-10-01 12:42:17 +0200111 mutex_lock(&wka_port->mutex);
112 if ((atomic_read(&wka_port->refcount) != 0) ||
Christof Schmittbd0072e2009-11-24 16:54:11 +0100113 (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
Swen Schillig5ab944f2008-10-01 12:42:17 +0200114 goto out;
115
Christof Schmittbd0072e2009-11-24 16:54:11 +0100116 wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200117 if (zfcp_fsf_close_wka_port(wka_port)) {
Christof Schmittbd0072e2009-11-24 16:54:11 +0100118 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200119 wake_up(&wka_port->completion_wq);
120 }
121out:
122 mutex_unlock(&wka_port->mutex);
123}
124
Christof Schmittbd0072e2009-11-24 16:54:11 +0100125static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200126{
127 if (atomic_dec_return(&wka_port->refcount) != 0)
128 return;
Martin Olsson19af5cd2009-04-23 11:37:37 +0200129 /* wait 10 milliseconds, other reqs might pop in */
Swen Schillig5ab944f2008-10-01 12:42:17 +0200130 schedule_delayed_work(&wka_port->work, HZ / 100);
131}
132
Christof Schmittbd0072e2009-11-24 16:54:11 +0100133static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
Sven Schuetz9d544f22009-04-06 18:31:47 +0200134 struct zfcp_adapter *adapter)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200135{
Swen Schillig5ab944f2008-10-01 12:42:17 +0200136 init_waitqueue_head(&wka_port->completion_wq);
137
138 wka_port->adapter = adapter;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200139 wka_port->d_id = d_id;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200140
Christof Schmittbd0072e2009-11-24 16:54:11 +0100141 wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200142 atomic_set(&wka_port->refcount, 0);
143 mutex_init(&wka_port->mutex);
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200144 INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200145}
146
Christof Schmittbd0072e2009-11-24 16:54:11 +0100147static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
Swen Schillig828bc122009-04-17 15:08:05 +0200148{
149 cancel_delayed_work_sync(&wka->work);
150 mutex_lock(&wka->mutex);
Christof Schmittbd0072e2009-11-24 16:54:11 +0100151 wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
Swen Schillig828bc122009-04-17 15:08:05 +0200152 mutex_unlock(&wka->mutex);
153}
154
Christof Schmittbd0072e2009-11-24 16:54:11 +0100155void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
Christof Schmitt55c770f2009-08-18 15:43:12 +0200156{
Swen Schilligf3450c72009-11-24 16:53:59 +0100157 if (!gs)
158 return;
Christof Schmitt55c770f2009-08-18 15:43:12 +0200159 zfcp_fc_wka_port_force_offline(&gs->ms);
160 zfcp_fc_wka_port_force_offline(&gs->ts);
161 zfcp_fc_wka_port_force_offline(&gs->ds);
162 zfcp_fc_wka_port_force_offline(&gs->as);
Christof Schmitt55c770f2009-08-18 15:43:12 +0200163}
164
Christof Schmitt24073b42008-06-10 18:20:54 +0200165static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100166 struct fc_els_rscn_page *page)
Christof Schmitt24073b42008-06-10 18:20:54 +0200167{
168 unsigned long flags;
Swen Schilligecf0c772009-11-24 16:53:58 +0100169 struct zfcp_adapter *adapter = fsf_req->adapter;
Christof Schmitt24073b42008-06-10 18:20:54 +0200170 struct zfcp_port *port;
171
Swen Schilligecf0c772009-11-24 16:53:58 +0100172 read_lock_irqsave(&adapter->port_list_lock, flags);
173 list_for_each_entry(port, &adapter->port_list, list) {
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100174 if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200175 zfcp_fc_test_link(port);
Swen Schilligea460a82009-05-15 13:18:20 +0200176 if (!port->d_id)
177 zfcp_erp_port_reopen(port,
178 ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100179 "fcrscn1");
Swen Schilligea460a82009-05-15 13:18:20 +0200180 }
Swen Schilligecf0c772009-11-24 16:53:58 +0100181 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200182}
183
184static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
185{
186 struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100187 struct fc_els_rscn *head;
188 struct fc_els_rscn_page *page;
Christof Schmitt24073b42008-06-10 18:20:54 +0200189 u16 i;
190 u16 no_entries;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100191 unsigned int afmt;
Christof Schmitt24073b42008-06-10 18:20:54 +0200192
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100193 head = (struct fc_els_rscn *) status_buffer->payload.data;
194 page = (struct fc_els_rscn_page *) head;
Christof Schmitt24073b42008-06-10 18:20:54 +0200195
196 /* see FC-FS */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100197 no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200198
199 for (i = 1; i < no_entries; i++) {
200 /* skip head and start with 1st element */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100201 page++;
202 afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
203 _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
204 page);
Sven Schuetz2d1e5472010-07-16 15:37:39 +0200205 zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
206 *(u32 *)page);
Christof Schmitt24073b42008-06-10 18:20:54 +0200207 }
Swen Schillig9eae07e2009-11-24 16:54:06 +0100208 queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
Christof Schmitt24073b42008-06-10 18:20:54 +0200209}
210
Swen Schillig7ba58c92008-10-01 12:42:18 +0200211static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200212{
Swen Schilligecf0c772009-11-24 16:53:58 +0100213 unsigned long flags;
Christof Schmitt24073b42008-06-10 18:20:54 +0200214 struct zfcp_adapter *adapter = req->adapter;
215 struct zfcp_port *port;
Christof Schmitt24073b42008-06-10 18:20:54 +0200216
Swen Schilligecf0c772009-11-24 16:53:58 +0100217 read_lock_irqsave(&adapter->port_list_lock, flags);
218 list_for_each_entry(port, &adapter->port_list, list)
219 if (port->wwpn == wwpn) {
Swen Schilligea4a3a62010-12-02 15:16:16 +0100220 zfcp_erp_port_forced_reopen(port, 0, "fciwwp1");
Christof Schmitt24073b42008-06-10 18:20:54 +0200221 break;
Swen Schilligecf0c772009-11-24 16:53:58 +0100222 }
223 read_unlock_irqrestore(&adapter->port_list_lock, flags);
Christof Schmitt24073b42008-06-10 18:20:54 +0200224}
225
226static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
227{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100228 struct fsf_status_read_buffer *status_buffer;
229 struct fc_els_flogi *plogi;
Christof Schmitt24073b42008-06-10 18:20:54 +0200230
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100231 status_buffer = (struct fsf_status_read_buffer *) req->data;
232 plogi = (struct fc_els_flogi *) status_buffer->payload.data;
233 zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200234}
235
236static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
237{
238 struct fsf_status_read_buffer *status_buffer =
239 (struct fsf_status_read_buffer *)req->data;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100240 struct fc_els_logo *logo =
241 (struct fc_els_logo *) status_buffer->payload.data;
Christof Schmitt24073b42008-06-10 18:20:54 +0200242
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100243 zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200244}
245
246/**
247 * zfcp_fc_incoming_els - handle incoming ELS
248 * @fsf_req - request which contains incoming ELS
249 */
250void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
251{
252 struct fsf_status_read_buffer *status_buffer =
253 (struct fsf_status_read_buffer *) fsf_req->data;
Swen Schilligc41f8cb2008-07-02 10:56:39 +0200254 unsigned int els_type = status_buffer->payload.data[0];
Christof Schmitt24073b42008-06-10 18:20:54 +0200255
Swen Schillig2c55b752010-12-02 15:16:13 +0100256 zfcp_dbf_san_in_els("fciels1", fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100257 if (els_type == ELS_PLOGI)
Christof Schmitt24073b42008-06-10 18:20:54 +0200258 zfcp_fc_incoming_plogi(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100259 else if (els_type == ELS_LOGO)
Christof Schmitt24073b42008-06-10 18:20:54 +0200260 zfcp_fc_incoming_logo(fsf_req);
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100261 else if (els_type == ELS_RSCN)
Christof Schmitt24073b42008-06-10 18:20:54 +0200262 zfcp_fc_incoming_rscn(fsf_req);
263}
264
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100265static void zfcp_fc_ns_gid_pn_eval(void *data)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200266{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100267 struct zfcp_fc_gid_pn *gid_pn = data;
268 struct zfcp_fsf_ct_els *ct = &gid_pn->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100269 struct zfcp_fc_gid_pn_req *gid_pn_req = sg_virt(ct->req);
270 struct zfcp_fc_gid_pn_resp *gid_pn_resp = sg_virt(ct->resp);
Christof Schmitt24073b42008-06-10 18:20:54 +0200271 struct zfcp_port *port = gid_pn->port;
272
273 if (ct->status)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200274 return;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100275 if (gid_pn_resp->ct_hdr.ct_cmd != FC_FS_ACC)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200276 return;
Christof Schmitta5b11dd2009-03-02 13:08:54 +0100277
Christof Schmitt24073b42008-06-10 18:20:54 +0200278 /* paranoia */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100279 if (gid_pn_req->gid_pn.fn_wwpn != port->wwpn)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200280 return;
Christof Schmitt24073b42008-06-10 18:20:54 +0200281 /* looks like a valid d_id */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100282 port->d_id = ntoh24(gid_pn_resp->gid_pn.fp_fid);
Christof Schmitt24073b42008-06-10 18:20:54 +0200283}
284
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100285static void zfcp_fc_complete(void *data)
286{
287 complete(data);
288}
289
Christof Schmitt799b76d2009-08-18 15:43:20 +0200290static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100291 struct zfcp_fc_gid_pn *gid_pn)
Christof Schmitt24073b42008-06-10 18:20:54 +0200292{
Christof Schmitt799b76d2009-08-18 15:43:20 +0200293 struct zfcp_adapter *adapter = port->adapter;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100294 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200295 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200296
297 /* setup parameters for send generic command */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200298 gid_pn->port = port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100299 gid_pn->ct.handler = zfcp_fc_complete;
300 gid_pn->ct.handler_data = &completion;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100301 gid_pn->ct.req = &gid_pn->sg_req;
302 gid_pn->ct.resp = &gid_pn->sg_resp;
303 sg_init_one(&gid_pn->sg_req, &gid_pn->gid_pn_req,
304 sizeof(struct zfcp_fc_gid_pn_req));
305 sg_init_one(&gid_pn->sg_resp, &gid_pn->gid_pn_resp,
306 sizeof(struct zfcp_fc_gid_pn_resp));
Christof Schmitt24073b42008-06-10 18:20:54 +0200307
308 /* setup nameserver request */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100309 gid_pn->gid_pn_req.ct_hdr.ct_rev = FC_CT_REV;
310 gid_pn->gid_pn_req.ct_hdr.ct_fs_type = FC_FST_DIR;
311 gid_pn->gid_pn_req.ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
312 gid_pn->gid_pn_req.ct_hdr.ct_options = 0;
313 gid_pn->gid_pn_req.ct_hdr.ct_cmd = FC_NS_GID_PN;
314 gid_pn->gid_pn_req.ct_hdr.ct_mr_size = ZFCP_FC_CT_SIZE_PAGE / 4;
315 gid_pn->gid_pn_req.gid_pn.fn_wwpn = port->wwpn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200316
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100317 ret = zfcp_fsf_send_ct(&adapter->gs->ds, &gid_pn->ct,
Swen Schillig51375ee2010-01-14 17:19:02 +0100318 adapter->pool.gid_pn_req,
319 ZFCP_FC_CTELS_TMO);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100320 if (!ret) {
321 wait_for_completion(&completion);
322 zfcp_fc_ns_gid_pn_eval(gid_pn);
323 }
Swen Schillig5ab944f2008-10-01 12:42:17 +0200324 return ret;
325}
326
327/**
328 * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
Christof Schmitt799b76d2009-08-18 15:43:20 +0200329 * @port: port where GID_PN request is needed
Swen Schillig5ab944f2008-10-01 12:42:17 +0200330 * return: -ENOMEM on error, 0 otherwise
331 */
Christof Schmitt799b76d2009-08-18 15:43:20 +0200332static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200333{
334 int ret;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100335 struct zfcp_fc_gid_pn *gid_pn;
Christof Schmitt799b76d2009-08-18 15:43:20 +0200336 struct zfcp_adapter *adapter = port->adapter;
Swen Schillig5ab944f2008-10-01 12:42:17 +0200337
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100338 gid_pn = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200339 if (!gid_pn)
340 return -ENOMEM;
341
342 memset(gid_pn, 0, sizeof(*gid_pn));
343
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200344 ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
Christof Schmitt24073b42008-06-10 18:20:54 +0200345 if (ret)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200346 goto out;
347
Christof Schmitt799b76d2009-08-18 15:43:20 +0200348 ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200349
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200350 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200351out:
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100352 mempool_free(gid_pn, adapter->pool.gid_pn);
Christof Schmitt24073b42008-06-10 18:20:54 +0200353 return ret;
354}
355
Christof Schmitt799b76d2009-08-18 15:43:20 +0200356void zfcp_fc_port_did_lookup(struct work_struct *work)
357{
358 int ret;
359 struct zfcp_port *port = container_of(work, struct zfcp_port,
360 gid_pn_work);
361
362 ret = zfcp_fc_ns_gid_pn(port);
363 if (ret) {
364 /* could not issue gid_pn for some reason */
Swen Schilligea4a3a62010-12-02 15:16:16 +0100365 zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1");
Christof Schmitt799b76d2009-08-18 15:43:20 +0200366 goto out;
367 }
368
369 if (!port->d_id) {
Swen Schilligedaed852010-09-08 14:40:01 +0200370 zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200371 goto out;
372 }
373
Swen Schilligea4a3a62010-12-02 15:16:16 +0100374 zfcp_erp_port_reopen(port, 0, "fcgpn_3");
Christof Schmitt799b76d2009-08-18 15:43:20 +0200375out:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100376 put_device(&port->dev);
Christof Schmitt799b76d2009-08-18 15:43:20 +0200377}
378
Christof Schmitt24073b42008-06-10 18:20:54 +0200379/**
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200380 * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
381 * @port: The zfcp_port to lookup the d_id for.
382 */
383void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
384{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100385 get_device(&port->dev);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200386 if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
Christof Schmitt615f59e2010-02-17 11:18:56 +0100387 put_device(&port->dev);
Christof Schmitt934aeb5872009-10-14 11:00:43 +0200388}
389
390/**
Christof Schmitt24073b42008-06-10 18:20:54 +0200391 * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
392 * @port: zfcp_port structure
393 * @plogi: plogi payload
394 *
395 * Evaluate PLOGI playload and copy important fields into zfcp_port structure
396 */
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100397void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
Christof Schmitt24073b42008-06-10 18:20:54 +0200398{
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100399 if (plogi->fl_wwpn != port->wwpn) {
400 port->d_id = 0;
401 dev_warn(&port->adapter->ccw_device->dev,
402 "A port opened with WWPN 0x%016Lx returned data that "
403 "identifies it as WWPN 0x%016Lx\n",
404 (unsigned long long) port->wwpn,
405 (unsigned long long) plogi->fl_wwpn);
406 return;
407 }
408
409 port->wwnn = plogi->fl_wwnn;
410 port->maxframe_size = plogi->fl_csp.sp_bb_data;
411
412 if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200413 port->supported_classes |= FC_COS_CLASS1;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100414 if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200415 port->supported_classes |= FC_COS_CLASS2;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100416 if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200417 port->supported_classes |= FC_COS_CLASS3;
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100418 if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
Christof Schmitt24073b42008-06-10 18:20:54 +0200419 port->supported_classes |= FC_COS_CLASS4;
420}
421
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100422static void zfcp_fc_adisc_handler(void *data)
Christof Schmitt24073b42008-06-10 18:20:54 +0200423{
Christof Schmitt087897e2011-02-22 19:54:41 +0100424 struct zfcp_fc_req *fc_req = data;
425 struct zfcp_port *port = fc_req->ct_els.port;
426 struct fc_els_adisc *adisc_resp = &fc_req->u.adisc.rsp;
Christof Schmitt24073b42008-06-10 18:20:54 +0200427
Christof Schmitt087897e2011-02-22 19:54:41 +0100428 if (fc_req->ct_els.status) {
Christof Schmitt24073b42008-06-10 18:20:54 +0200429 /* request rejected or timed out */
Swen Schillig5b43e712009-04-17 15:08:10 +0200430 zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100431 "fcadh_1");
Christof Schmitt24073b42008-06-10 18:20:54 +0200432 goto out;
433 }
434
435 if (!port->wwnn)
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100436 port->wwnn = adisc_resp->adisc_wwnn;
Christof Schmitt24073b42008-06-10 18:20:54 +0200437
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100438 if ((port->wwpn != adisc_resp->adisc_wwpn) ||
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100439 !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
Swen Schillig24095492009-03-02 13:09:07 +0100440 zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
Swen Schilligea4a3a62010-12-02 15:16:16 +0100441 "fcadh_2");
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100442 goto out;
443 }
Christof Schmitt24073b42008-06-10 18:20:54 +0200444
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100445 /* port is good, unblock rport without going through erp */
446 zfcp_scsi_schedule_rport_register(port);
Christof Schmitt24073b42008-06-10 18:20:54 +0200447 out:
Christof Schmitt14e242e2009-08-18 15:43:11 +0200448 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Christof Schmitt615f59e2010-02-17 11:18:56 +0100449 put_device(&port->dev);
Christof Schmitt087897e2011-02-22 19:54:41 +0100450 kmem_cache_free(zfcp_fc_req_cache, fc_req);
Christof Schmitt24073b42008-06-10 18:20:54 +0200451}
452
453static int zfcp_fc_adisc(struct zfcp_port *port)
454{
Christof Schmitt087897e2011-02-22 19:54:41 +0100455 struct zfcp_fc_req *fc_req;
Christof Schmitt24073b42008-06-10 18:20:54 +0200456 struct zfcp_adapter *adapter = port->adapter;
Christof Schmitt087897e2011-02-22 19:54:41 +0100457 struct Scsi_Host *shost = adapter->scsi_host;
Christof Schmittee744622009-11-24 16:54:14 +0100458 int ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200459
Christof Schmitt087897e2011-02-22 19:54:41 +0100460 fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC);
461 if (!fc_req)
Christof Schmitt24073b42008-06-10 18:20:54 +0200462 return -ENOMEM;
463
Christof Schmitt087897e2011-02-22 19:54:41 +0100464 fc_req->ct_els.port = port;
465 fc_req->ct_els.req = &fc_req->sg_req;
466 fc_req->ct_els.resp = &fc_req->sg_rsp;
467 sg_init_one(&fc_req->sg_req, &fc_req->u.adisc.req,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100468 sizeof(struct fc_els_adisc));
Christof Schmitt087897e2011-02-22 19:54:41 +0100469 sg_init_one(&fc_req->sg_rsp, &fc_req->u.adisc.rsp,
Christof Schmitt9d05ce22009-11-24 16:54:09 +0100470 sizeof(struct fc_els_adisc));
Christof Schmitt24073b42008-06-10 18:20:54 +0200471
Christof Schmitt087897e2011-02-22 19:54:41 +0100472 fc_req->ct_els.handler = zfcp_fc_adisc_handler;
473 fc_req->ct_els.handler_data = fc_req;
Christof Schmitt24073b42008-06-10 18:20:54 +0200474
475 /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
476 without FC-AL-2 capability, so we don't set it */
Christof Schmitt087897e2011-02-22 19:54:41 +0100477 fc_req->u.adisc.req.adisc_wwpn = fc_host_port_name(shost);
478 fc_req->u.adisc.req.adisc_wwnn = fc_host_node_name(shost);
479 fc_req->u.adisc.req.adisc_cmd = ELS_ADISC;
480 hton24(fc_req->u.adisc.req.adisc_port_id, fc_host_port_id(shost));
Christof Schmitt24073b42008-06-10 18:20:54 +0200481
Christof Schmitt087897e2011-02-22 19:54:41 +0100482 ret = zfcp_fsf_send_els(adapter, port->d_id, &fc_req->ct_els,
Swen Schillig51375ee2010-01-14 17:19:02 +0100483 ZFCP_FC_CTELS_TMO);
Christof Schmittee744622009-11-24 16:54:14 +0100484 if (ret)
Christof Schmitt087897e2011-02-22 19:54:41 +0100485 kmem_cache_free(zfcp_fc_req_cache, fc_req);
Christof Schmittee744622009-11-24 16:54:14 +0100486
487 return ret;
Christof Schmitt24073b42008-06-10 18:20:54 +0200488}
489
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100490void zfcp_fc_link_test_work(struct work_struct *work)
491{
492 struct zfcp_port *port =
493 container_of(work, struct zfcp_port, test_link_work);
494 int retval;
495
Christof Schmitt615f59e2010-02-17 11:18:56 +0100496 get_device(&port->dev);
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100497 port->rport_task = RPORT_DEL;
498 zfcp_scsi_rport_work(&port->rport_work);
499
Christof Schmitt14e242e2009-08-18 15:43:11 +0200500 /* only issue one test command at one time per port */
501 if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
502 goto out;
503
504 atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
505
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100506 retval = zfcp_fc_adisc(port);
507 if (retval == 0)
508 return;
509
510 /* send of ADISC was not possible */
Christof Schmitt14e242e2009-08-18 15:43:11 +0200511 atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
Swen Schilligea4a3a62010-12-02 15:16:16 +0100512 zfcp_erp_port_forced_reopen(port, 0, "fcltwk1");
Christof Schmitta2fa0ae2009-03-02 13:09:08 +0100513
Christof Schmitt14e242e2009-08-18 15:43:11 +0200514out:
Christof Schmitt615f59e2010-02-17 11:18:56 +0100515 put_device(&port->dev);
Christof Schmitt8fdf30d2009-03-02 13:09:01 +0100516}
517
Christof Schmitt24073b42008-06-10 18:20:54 +0200518/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200519 * zfcp_fc_test_link - lightweight link test procedure
Christof Schmitt24073b42008-06-10 18:20:54 +0200520 * @port: port to be tested
521 *
522 * Test status of a link to a remote port using the ELS command ADISC.
523 * If there is a problem with the remote port, error recovery steps
524 * will be triggered.
525 */
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200526void zfcp_fc_test_link(struct zfcp_port *port)
Christof Schmitt24073b42008-06-10 18:20:54 +0200527{
Christof Schmitt615f59e2010-02-17 11:18:56 +0100528 get_device(&port->dev);
Swen Schillig45446832009-08-18 15:43:17 +0200529 if (!queue_work(port->adapter->work_queue, &port->test_link_work))
Christof Schmitt615f59e2010-02-17 11:18:56 +0100530 put_device(&port->dev);
Christof Schmitt24073b42008-06-10 18:20:54 +0200531}
Swen Schilligcc8c2822008-06-10 18:21:00 +0200532
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100533static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200534{
535 struct scatterlist *sg = &gpn_ft->sg_req;
536
Swen Schilliga4623c42009-08-18 15:43:15 +0200537 kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100538 zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200539
540 kfree(gpn_ft);
541}
542
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100543static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200544{
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100545 struct zfcp_fc_gpn_ft *gpn_ft;
546 struct zfcp_fc_gpn_ft_req *req;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200547
548 gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
549 if (!gpn_ft)
550 return NULL;
551
Christof Schmitt6e51f082010-04-30 18:09:37 +0200552 req = kmem_cache_zalloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200553 if (!req) {
554 kfree(gpn_ft);
555 gpn_ft = NULL;
556 goto out;
557 }
558 sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
559
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100560 if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
561 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200562 gpn_ft = NULL;
563 }
564out:
565 return gpn_ft;
566}
567
568
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100569static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200570 struct zfcp_adapter *adapter, int max_bytes)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200571{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100572 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100573 struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100574 DECLARE_COMPLETION_ONSTACK(completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200575 int ret;
576
577 /* prepare CT IU for GPN_FT */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100578 req->ct_hdr.ct_rev = FC_CT_REV;
579 req->ct_hdr.ct_fs_type = FC_FST_DIR;
580 req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
581 req->ct_hdr.ct_options = 0;
582 req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
583 req->ct_hdr.ct_mr_size = max_bytes / 4;
584 req->gpn_ft.fn_domain_id_scope = 0;
585 req->gpn_ft.fn_area_id_scope = 0;
586 req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200587
588 /* prepare zfcp_send_ct */
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100589 ct->handler = zfcp_fc_complete;
590 ct->handler_data = &completion;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200591 ct->req = &gpn_ft->sg_req;
592 ct->resp = gpn_ft->sg_resp;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200593
Swen Schillig51375ee2010-01-14 17:19:02 +0100594 ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct, NULL,
595 ZFCP_FC_CTELS_TMO);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200596 if (!ret)
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100597 wait_for_completion(&completion);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200598 return ret;
599}
600
Swen Schilligf3450c72009-11-24 16:53:59 +0100601static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200602{
Martin Petermann6ab35c02009-04-17 15:08:13 +0200603 if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
604 return;
605
Swen Schilligcc8c2822008-06-10 18:21:00 +0200606 atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
607
Christof Schmitt04062892008-10-01 12:42:20 +0200608 if ((port->supported_classes != 0) ||
Swen Schilligf3450c72009-11-24 16:53:59 +0100609 !list_empty(&port->unit_list))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200610 return;
Swen Schilligf3450c72009-11-24 16:53:59 +0100611
Swen Schilligf3450c72009-11-24 16:53:59 +0100612 list_move_tail(&port->list, lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200613}
614
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100615static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
616 struct zfcp_adapter *adapter, int max_entries)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200617{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100618 struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200619 struct scatterlist *sg = gpn_ft->sg_resp;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100620 struct fc_ct_hdr *hdr = sg_virt(sg);
621 struct fc_gpn_ft_resp *acc = sg_virt(sg);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200622 struct zfcp_port *port, *tmp;
Swen Schilligecf0c772009-11-24 16:53:58 +0100623 unsigned long flags;
Swen Schilligf3450c72009-11-24 16:53:59 +0100624 LIST_HEAD(remove_lh);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200625 u32 d_id;
Christof Schmitt47f7bba2008-08-21 13:43:33 +0200626 int ret = 0, x, last = 0;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200627
628 if (ct->status)
629 return -EIO;
630
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100631 if (hdr->ct_cmd != FC_FS_ACC) {
632 if (hdr->ct_reason == FC_BA_RJT_UNABLE)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200633 return -EAGAIN; /* might be a temporary condition */
634 return -EIO;
635 }
636
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100637 if (hdr->ct_mr_size) {
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100638 dev_warn(&adapter->ccw_device->dev,
639 "The name server reported %d words residual data\n",
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100640 hdr->ct_mr_size);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200641 return -E2BIG;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100642 }
Swen Schilligcc8c2822008-06-10 18:21:00 +0200643
Swen Schilligcc8c2822008-06-10 18:21:00 +0200644 /* first entry is the header */
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100645 for (x = 1; x < max_entries && !last; x++) {
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100646 if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200647 acc++;
648 else
649 acc = sg_virt(++sg);
650
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100651 last = acc->fp_flags & FC_NS_FID_LAST;
652 d_id = ntoh24(acc->fp_fid);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200653
Swen Schillig5ab944f2008-10-01 12:42:17 +0200654 /* don't attach ports with a well known address */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100655 if (d_id >= FC_FID_WELL_KNOWN_BASE)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200656 continue;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200657 /* skip the adapter's port and known remote ports */
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100658 if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
Swen Schilligcc8c2822008-06-10 18:21:00 +0200659 continue;
660
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100661 port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
Swen Schilligcc8c2822008-06-10 18:21:00 +0200662 ZFCP_STATUS_COMMON_NOESC, d_id);
Swen Schilligecf0c772009-11-24 16:53:58 +0100663 if (!IS_ERR(port))
Swen Schilligea4a3a62010-12-02 15:16:16 +0100664 zfcp_erp_port_reopen(port, 0, "fcegpf1");
Swen Schilligecf0c772009-11-24 16:53:58 +0100665 else if (PTR_ERR(port) != -EEXIST)
666 ret = PTR_ERR(port);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200667 }
668
669 zfcp_erp_wait(adapter);
Swen Schilligecf0c772009-11-24 16:53:58 +0100670 write_lock_irqsave(&adapter->port_list_lock, flags);
671 list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
Swen Schilligf3450c72009-11-24 16:53:59 +0100672 zfcp_fc_validate_port(port, &remove_lh);
Swen Schilligecf0c772009-11-24 16:53:58 +0100673 write_unlock_irqrestore(&adapter->port_list_lock, flags);
Swen Schilligf3450c72009-11-24 16:53:59 +0100674
675 list_for_each_entry_safe(port, tmp, &remove_lh, list) {
Swen Schilligea4a3a62010-12-02 15:16:16 +0100676 zfcp_erp_port_shutdown(port, 0, "fcegpf2");
Christof Schmitt615f59e2010-02-17 11:18:56 +0100677 zfcp_device_unregister(&port->dev, &zfcp_sysfs_port_attrs);
Swen Schilligf3450c72009-11-24 16:53:59 +0100678 }
679
Swen Schilligcc8c2822008-06-10 18:21:00 +0200680 return ret;
681}
682
683/**
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200684 * zfcp_fc_scan_ports - scan remote ports and attach new ports
Swen Schillig9eae07e2009-11-24 16:54:06 +0100685 * @work: reference to scheduled work
Swen Schilligcc8c2822008-06-10 18:21:00 +0200686 */
Swen Schillig9eae07e2009-11-24 16:54:06 +0100687void zfcp_fc_scan_ports(struct work_struct *work)
Swen Schilligcc8c2822008-06-10 18:21:00 +0200688{
Swen Schillig9eae07e2009-11-24 16:54:06 +0100689 struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
690 scan_work);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200691 int ret, i;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100692 struct zfcp_fc_gpn_ft *gpn_ft;
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100693 int chain, max_entries, buf_num, max_bytes;
694
695 chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
Christof Schmittdbf5dfe2009-11-24 16:54:10 +0100696 buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
697 max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
698 max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200699
Swen Schillig306b6ed2009-04-17 15:08:02 +0200700 if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
701 fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
Swen Schillig9eae07e2009-11-24 16:54:06 +0100702 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200703
Swen Schillig9eae07e2009-11-24 16:54:06 +0100704 if (zfcp_fc_wka_port_get(&adapter->gs->ds))
705 return;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200706
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100707 gpn_ft = zfcp_alloc_sg_env(buf_num);
Swen Schillig9eae07e2009-11-24 16:54:06 +0100708 if (!gpn_ft)
Swen Schillig5ab944f2008-10-01 12:42:17 +0200709 goto out;
Swen Schilligcc8c2822008-06-10 18:21:00 +0200710
711 for (i = 0; i < 3; i++) {
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200712 ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200713 if (!ret) {
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100714 ret = zfcp_fc_eval_gpn_ft(gpn_ft, adapter, max_entries);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200715 if (ret == -EAGAIN)
716 ssleep(1);
717 else
718 break;
719 }
720 }
Christof Schmitt39eb7e9a2008-12-19 16:57:01 +0100721 zfcp_free_sg_env(gpn_ft, buf_num);
Swen Schillig5ab944f2008-10-01 12:42:17 +0200722out:
Swen Schillig6f53a2d2009-08-18 15:43:23 +0200723 zfcp_fc_wka_port_put(&adapter->gs->ds);
Swen Schilligcc8c2822008-06-10 18:21:00 +0200724}
725
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100726static void zfcp_fc_ct_els_job_handler(void *data)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200727{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100728 struct fc_bsg_job *job = data;
729 struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
Swen Schillig7dec9cf2010-01-26 17:49:19 +0100730 struct fc_bsg_reply *jr = job->reply;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200731
Swen Schillig7dec9cf2010-01-26 17:49:19 +0100732 jr->reply_payload_rcv_len = job->reply_payload.payload_len;
733 jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
734 jr->result = zfcp_ct_els->status ? -EIO : 0;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200735 job->job_done(job);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200736}
737
Christof Schmittf09d5452010-01-13 17:52:36 +0100738static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct fc_bsg_job *job)
739{
740 u32 preamble_word1;
741 u8 gs_type;
742 struct zfcp_adapter *adapter;
743
744 preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
745 gs_type = (preamble_word1 & 0xff000000) >> 24;
746
747 adapter = (struct zfcp_adapter *) job->shost->hostdata[0];
748
749 switch (gs_type) {
750 case FC_FST_ALIAS:
751 return &adapter->gs->as;
752 case FC_FST_MGMT:
753 return &adapter->gs->ms;
754 case FC_FST_TIME:
755 return &adapter->gs->ts;
756 break;
757 case FC_FST_DIR:
758 return &adapter->gs->ds;
759 break;
760 default:
761 return NULL;
762 }
763}
764
765static void zfcp_fc_ct_job_handler(void *data)
766{
767 struct fc_bsg_job *job = data;
768 struct zfcp_fc_wka_port *wka_port;
769
770 wka_port = zfcp_fc_job_wka_port(job);
771 zfcp_fc_wka_port_put(wka_port);
772
773 zfcp_fc_ct_els_job_handler(data);
774}
775
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100776static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
777 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200778{
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100779 struct zfcp_fsf_ct_els *els = job->dd_data;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200780 struct fc_rport *rport = job->rport;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200781 struct zfcp_port *port;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100782 u32 d_id;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200783
Sven Schuetz9d544f22009-04-06 18:31:47 +0200784 if (rport) {
Swen Schilligea945ff2009-08-18 15:43:24 +0200785 port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100786 if (!port)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200787 return -EINVAL;
Swen Schilligecf0c772009-11-24 16:53:58 +0100788
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100789 d_id = port->d_id;
Christof Schmitt615f59e2010-02-17 11:18:56 +0100790 put_device(&port->dev);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100791 } else
792 d_id = ntoh24(job->request->rqst_data.h_els.port_id);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200793
Christof Schmittf09d5452010-01-13 17:52:36 +0100794 els->handler = zfcp_fc_ct_els_job_handler;
Swen Schillig51375ee2010-01-14 17:19:02 +0100795 return zfcp_fsf_send_els(adapter, d_id, els, job->req->timeout / HZ);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200796}
797
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100798static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
799 struct zfcp_adapter *adapter)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200800{
801 int ret;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100802 struct zfcp_fsf_ct_els *ct = job->dd_data;
803 struct zfcp_fc_wka_port *wka_port;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200804
Christof Schmittf09d5452010-01-13 17:52:36 +0100805 wka_port = zfcp_fc_job_wka_port(job);
806 if (!wka_port)
807 return -EINVAL;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200808
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100809 ret = zfcp_fc_wka_port_get(wka_port);
810 if (ret)
Sven Schuetz9d544f22009-04-06 18:31:47 +0200811 return ret;
Sven Schuetz9d544f22009-04-06 18:31:47 +0200812
Christof Schmittf09d5452010-01-13 17:52:36 +0100813 ct->handler = zfcp_fc_ct_job_handler;
Swen Schillig51375ee2010-01-14 17:19:02 +0100814 ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->req->timeout / HZ);
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100815 if (ret)
816 zfcp_fc_wka_port_put(wka_port);
Sven Schuetz9d544f22009-04-06 18:31:47 +0200817
Sven Schuetz9d544f22009-04-06 18:31:47 +0200818 return ret;
819}
Swen Schilligd5a282a2009-08-18 15:43:22 +0200820
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100821int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
822{
823 struct Scsi_Host *shost;
824 struct zfcp_adapter *adapter;
825 struct zfcp_fsf_ct_els *ct_els = job->dd_data;
826
827 shost = job->rport ? rport_to_shost(job->rport) : job->shost;
828 adapter = (struct zfcp_adapter *)shost->hostdata[0];
829
830 if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
831 return -EINVAL;
832
833 ct_els->req = job->request_payload.sg_list;
834 ct_els->resp = job->reply_payload.sg_list;
Christof Schmitt7c7dc192009-11-24 16:54:13 +0100835 ct_els->handler_data = job;
836
837 switch (job->request->msgcode) {
838 case FC_BSG_RPT_ELS:
839 case FC_BSG_HST_ELS_NOLOGIN:
840 return zfcp_fc_exec_els_job(job, adapter);
841 case FC_BSG_RPT_CT:
842 case FC_BSG_HST_CT:
843 return zfcp_fc_exec_ct_job(job, adapter);
844 default:
845 return -EINVAL;
846 }
847}
848
Swen Schillig491ca442010-01-14 17:19:01 +0100849int zfcp_fc_timeout_bsg_job(struct fc_bsg_job *job)
850{
851 /* hardware tracks timeout, reset bsg timeout to not interfere */
852 return -EAGAIN;
853}
854
Swen Schilligd5a282a2009-08-18 15:43:22 +0200855int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
856{
Christof Schmittbd0072e2009-11-24 16:54:11 +0100857 struct zfcp_fc_wka_ports *wka_ports;
Swen Schilligd5a282a2009-08-18 15:43:22 +0200858
Christof Schmittbd0072e2009-11-24 16:54:11 +0100859 wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200860 if (!wka_ports)
861 return -ENOMEM;
862
863 adapter->gs = wka_ports;
864 zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
865 zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
866 zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
867 zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
Swen Schilligd5a282a2009-08-18 15:43:22 +0200868
869 return 0;
870}
871
872void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
873{
874 kfree(adapter->gs);
875 adapter->gs = NULL;
876}
877