zfcp_fc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * zfcp device driver
  3. *
  4. * Fibre Channel related functions for the zfcp device driver.
  5. *
  6. * Copyright IBM Corporation 2008, 2009
  7. */
  8. #define KMSG_COMPONENT "zfcp"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/types.h>
  11. #include <scsi/fc/fc_els.h>
  12. #include <scsi/libfc.h>
  13. #include "zfcp_ext.h"
  14. #include "zfcp_fc.h"
  15. static u32 zfcp_fc_rscn_range_mask[] = {
  16. [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
  17. [ELS_ADDR_FMT_AREA] = 0xFFFF00,
  18. [ELS_ADDR_FMT_DOM] = 0xFF0000,
  19. [ELS_ADDR_FMT_FAB] = 0x000000,
  20. };
  21. static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
  22. {
  23. if (mutex_lock_interruptible(&wka_port->mutex))
  24. return -ERESTARTSYS;
  25. if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
  26. wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
  27. wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
  28. if (zfcp_fsf_open_wka_port(wka_port))
  29. wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
  30. }
  31. mutex_unlock(&wka_port->mutex);
  32. wait_event(wka_port->completion_wq,
  33. wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
  34. wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
  35. if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
  36. atomic_inc(&wka_port->refcount);
  37. return 0;
  38. }
  39. return -EIO;
  40. }
  41. static void zfcp_fc_wka_port_offline(struct work_struct *work)
  42. {
  43. struct delayed_work *dw = to_delayed_work(work);
  44. struct zfcp_fc_wka_port *wka_port =
  45. container_of(dw, struct zfcp_fc_wka_port, work);
  46. mutex_lock(&wka_port->mutex);
  47. if ((atomic_read(&wka_port->refcount) != 0) ||
  48. (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
  49. goto out;
  50. wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
  51. if (zfcp_fsf_close_wka_port(wka_port)) {
  52. wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
  53. wake_up(&wka_port->completion_wq);
  54. }
  55. out:
  56. mutex_unlock(&wka_port->mutex);
  57. }
  58. static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
  59. {
  60. if (atomic_dec_return(&wka_port->refcount) != 0)
  61. return;
  62. /* wait 10 milliseconds, other reqs might pop in */
  63. schedule_delayed_work(&wka_port->work, HZ / 100);
  64. }
  65. static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
  66. struct zfcp_adapter *adapter)
  67. {
  68. init_waitqueue_head(&wka_port->completion_wq);
  69. wka_port->adapter = adapter;
  70. wka_port->d_id = d_id;
  71. wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
  72. atomic_set(&wka_port->refcount, 0);
  73. mutex_init(&wka_port->mutex);
  74. INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
  75. }
  76. static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
  77. {
  78. cancel_delayed_work_sync(&wka->work);
  79. mutex_lock(&wka->mutex);
  80. wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
  81. mutex_unlock(&wka->mutex);
  82. }
  83. void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
  84. {
  85. if (!gs)
  86. return;
  87. zfcp_fc_wka_port_force_offline(&gs->ms);
  88. zfcp_fc_wka_port_force_offline(&gs->ts);
  89. zfcp_fc_wka_port_force_offline(&gs->ds);
  90. zfcp_fc_wka_port_force_offline(&gs->as);
  91. }
  92. static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
  93. struct fc_els_rscn_page *page)
  94. {
  95. unsigned long flags;
  96. struct zfcp_adapter *adapter = fsf_req->adapter;
  97. struct zfcp_port *port;
  98. read_lock_irqsave(&adapter->port_list_lock, flags);
  99. list_for_each_entry(port, &adapter->port_list, list) {
  100. if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
  101. zfcp_fc_test_link(port);
  102. if (!port->d_id)
  103. zfcp_erp_port_reopen(port,
  104. ZFCP_STATUS_COMMON_ERP_FAILED,
  105. "fcrscn1", NULL);
  106. }
  107. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  108. }
  109. static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
  110. {
  111. struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
  112. struct fc_els_rscn *head;
  113. struct fc_els_rscn_page *page;
  114. u16 i;
  115. u16 no_entries;
  116. unsigned int afmt;
  117. head = (struct fc_els_rscn *) status_buffer->payload.data;
  118. page = (struct fc_els_rscn_page *) head;
  119. /* see FC-FS */
  120. no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
  121. for (i = 1; i < no_entries; i++) {
  122. /* skip head and start with 1st element */
  123. page++;
  124. afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
  125. _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
  126. page);
  127. }
  128. queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
  129. }
  130. static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
  131. {
  132. unsigned long flags;
  133. struct zfcp_adapter *adapter = req->adapter;
  134. struct zfcp_port *port;
  135. read_lock_irqsave(&adapter->port_list_lock, flags);
  136. list_for_each_entry(port, &adapter->port_list, list)
  137. if (port->wwpn == wwpn) {
  138. zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
  139. break;
  140. }
  141. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  142. }
  143. static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
  144. {
  145. struct fsf_status_read_buffer *status_buffer;
  146. struct fc_els_flogi *plogi;
  147. status_buffer = (struct fsf_status_read_buffer *) req->data;
  148. plogi = (struct fc_els_flogi *) status_buffer->payload.data;
  149. zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
  150. }
  151. static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
  152. {
  153. struct fsf_status_read_buffer *status_buffer =
  154. (struct fsf_status_read_buffer *)req->data;
  155. struct fc_els_logo *logo =
  156. (struct fc_els_logo *) status_buffer->payload.data;
  157. zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
  158. }
  159. /**
  160. * zfcp_fc_incoming_els - handle incoming ELS
  161. * @fsf_req - request which contains incoming ELS
  162. */
  163. void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
  164. {
  165. struct fsf_status_read_buffer *status_buffer =
  166. (struct fsf_status_read_buffer *) fsf_req->data;
  167. unsigned int els_type = status_buffer->payload.data[0];
  168. zfcp_dbf_san_incoming_els(fsf_req);
  169. if (els_type == ELS_PLOGI)
  170. zfcp_fc_incoming_plogi(fsf_req);
  171. else if (els_type == ELS_LOGO)
  172. zfcp_fc_incoming_logo(fsf_req);
  173. else if (els_type == ELS_RSCN)
  174. zfcp_fc_incoming_rscn(fsf_req);
  175. }
  176. static void zfcp_fc_ns_gid_pn_eval(void *data)
  177. {
  178. struct zfcp_fc_gid_pn *gid_pn = data;
  179. struct zfcp_fsf_ct_els *ct = &gid_pn->ct;
  180. struct zfcp_fc_gid_pn_req *gid_pn_req = sg_virt(ct->req);
  181. struct zfcp_fc_gid_pn_resp *gid_pn_resp = sg_virt(ct->resp);
  182. struct zfcp_port *port = gid_pn->port;
  183. if (ct->status)
  184. return;
  185. if (gid_pn_resp->ct_hdr.ct_cmd != FC_FS_ACC)
  186. return;
  187. /* paranoia */
  188. if (gid_pn_req->gid_pn.fn_wwpn != port->wwpn)
  189. return;
  190. /* looks like a valid d_id */
  191. port->d_id = ntoh24(gid_pn_resp->gid_pn.fp_fid);
  192. }
  193. static void zfcp_fc_complete(void *data)
  194. {
  195. complete(data);
  196. }
  197. static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
  198. struct zfcp_fc_gid_pn *gid_pn)
  199. {
  200. struct zfcp_adapter *adapter = port->adapter;
  201. DECLARE_COMPLETION_ONSTACK(completion);
  202. int ret;
  203. /* setup parameters for send generic command */
  204. gid_pn->port = port;
  205. gid_pn->ct.handler = zfcp_fc_complete;
  206. gid_pn->ct.handler_data = &completion;
  207. gid_pn->ct.req = &gid_pn->sg_req;
  208. gid_pn->ct.resp = &gid_pn->sg_resp;
  209. sg_init_one(&gid_pn->sg_req, &gid_pn->gid_pn_req,
  210. sizeof(struct zfcp_fc_gid_pn_req));
  211. sg_init_one(&gid_pn->sg_resp, &gid_pn->gid_pn_resp,
  212. sizeof(struct zfcp_fc_gid_pn_resp));
  213. /* setup nameserver request */
  214. gid_pn->gid_pn_req.ct_hdr.ct_rev = FC_CT_REV;
  215. gid_pn->gid_pn_req.ct_hdr.ct_fs_type = FC_FST_DIR;
  216. gid_pn->gid_pn_req.ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
  217. gid_pn->gid_pn_req.ct_hdr.ct_options = 0;
  218. gid_pn->gid_pn_req.ct_hdr.ct_cmd = FC_NS_GID_PN;
  219. gid_pn->gid_pn_req.ct_hdr.ct_mr_size = ZFCP_FC_CT_SIZE_PAGE / 4;
  220. gid_pn->gid_pn_req.gid_pn.fn_wwpn = port->wwpn;
  221. ret = zfcp_fsf_send_ct(&adapter->gs->ds, &gid_pn->ct,
  222. adapter->pool.gid_pn_req);
  223. if (!ret) {
  224. wait_for_completion(&completion);
  225. zfcp_fc_ns_gid_pn_eval(gid_pn);
  226. }
  227. return ret;
  228. }
  229. /**
  230. * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
  231. * @port: port where GID_PN request is needed
  232. * return: -ENOMEM on error, 0 otherwise
  233. */
  234. static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
  235. {
  236. int ret;
  237. struct zfcp_fc_gid_pn *gid_pn;
  238. struct zfcp_adapter *adapter = port->adapter;
  239. gid_pn = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
  240. if (!gid_pn)
  241. return -ENOMEM;
  242. memset(gid_pn, 0, sizeof(*gid_pn));
  243. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  244. if (ret)
  245. goto out;
  246. ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
  247. zfcp_fc_wka_port_put(&adapter->gs->ds);
  248. out:
  249. mempool_free(gid_pn, adapter->pool.gid_pn);
  250. return ret;
  251. }
  252. void zfcp_fc_port_did_lookup(struct work_struct *work)
  253. {
  254. int ret;
  255. struct zfcp_port *port = container_of(work, struct zfcp_port,
  256. gid_pn_work);
  257. ret = zfcp_fc_ns_gid_pn(port);
  258. if (ret) {
  259. /* could not issue gid_pn for some reason */
  260. zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
  261. goto out;
  262. }
  263. if (!port->d_id) {
  264. zfcp_erp_port_failed(port, "fcgpn_2", NULL);
  265. goto out;
  266. }
  267. zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
  268. out:
  269. put_device(&port->sysfs_device);
  270. }
  271. /**
  272. * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
  273. * @port: The zfcp_port to lookup the d_id for.
  274. */
  275. void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
  276. {
  277. get_device(&port->sysfs_device);
  278. if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
  279. put_device(&port->sysfs_device);
  280. }
  281. /**
  282. * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
  283. * @port: zfcp_port structure
  284. * @plogi: plogi payload
  285. *
  286. * Evaluate PLOGI playload and copy important fields into zfcp_port structure
  287. */
  288. void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
  289. {
  290. if (plogi->fl_wwpn != port->wwpn) {
  291. port->d_id = 0;
  292. dev_warn(&port->adapter->ccw_device->dev,
  293. "A port opened with WWPN 0x%016Lx returned data that "
  294. "identifies it as WWPN 0x%016Lx\n",
  295. (unsigned long long) port->wwpn,
  296. (unsigned long long) plogi->fl_wwpn);
  297. return;
  298. }
  299. port->wwnn = plogi->fl_wwnn;
  300. port->maxframe_size = plogi->fl_csp.sp_bb_data;
  301. if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
  302. port->supported_classes |= FC_COS_CLASS1;
  303. if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
  304. port->supported_classes |= FC_COS_CLASS2;
  305. if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
  306. port->supported_classes |= FC_COS_CLASS3;
  307. if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
  308. port->supported_classes |= FC_COS_CLASS4;
  309. }
  310. static void zfcp_fc_adisc_handler(void *data)
  311. {
  312. struct zfcp_fc_els_adisc *adisc = data;
  313. struct zfcp_port *port = adisc->els.port;
  314. struct fc_els_adisc *adisc_resp = &adisc->adisc_resp;
  315. if (adisc->els.status) {
  316. /* request rejected or timed out */
  317. zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  318. "fcadh_1", NULL);
  319. goto out;
  320. }
  321. if (!port->wwnn)
  322. port->wwnn = adisc_resp->adisc_wwnn;
  323. if ((port->wwpn != adisc_resp->adisc_wwpn) ||
  324. !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
  325. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  326. "fcadh_2", NULL);
  327. goto out;
  328. }
  329. /* port is good, unblock rport without going through erp */
  330. zfcp_scsi_schedule_rport_register(port);
  331. out:
  332. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  333. put_device(&port->sysfs_device);
  334. kmem_cache_free(zfcp_data.adisc_cache, adisc);
  335. }
  336. static int zfcp_fc_adisc(struct zfcp_port *port)
  337. {
  338. struct zfcp_fc_els_adisc *adisc;
  339. struct zfcp_adapter *adapter = port->adapter;
  340. int ret;
  341. adisc = kmem_cache_alloc(zfcp_data.adisc_cache, GFP_ATOMIC);
  342. if (!adisc)
  343. return -ENOMEM;
  344. adisc->els.port = port;
  345. adisc->els.req = &adisc->req;
  346. adisc->els.resp = &adisc->resp;
  347. sg_init_one(adisc->els.req, &adisc->adisc_req,
  348. sizeof(struct fc_els_adisc));
  349. sg_init_one(adisc->els.resp, &adisc->adisc_resp,
  350. sizeof(struct fc_els_adisc));
  351. adisc->els.handler = zfcp_fc_adisc_handler;
  352. adisc->els.handler_data = adisc;
  353. /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
  354. without FC-AL-2 capability, so we don't set it */
  355. adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
  356. adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
  357. adisc->adisc_req.adisc_cmd = ELS_ADISC;
  358. hton24(adisc->adisc_req.adisc_port_id,
  359. fc_host_port_id(adapter->scsi_host));
  360. ret = zfcp_fsf_send_els(adapter, port->d_id, &adisc->els);
  361. if (ret)
  362. kmem_cache_free(zfcp_data.adisc_cache, adisc);
  363. return ret;
  364. }
  365. void zfcp_fc_link_test_work(struct work_struct *work)
  366. {
  367. struct zfcp_port *port =
  368. container_of(work, struct zfcp_port, test_link_work);
  369. int retval;
  370. get_device(&port->sysfs_device);
  371. port->rport_task = RPORT_DEL;
  372. zfcp_scsi_rport_work(&port->rport_work);
  373. /* only issue one test command at one time per port */
  374. if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
  375. goto out;
  376. atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  377. retval = zfcp_fc_adisc(port);
  378. if (retval == 0)
  379. return;
  380. /* send of ADISC was not possible */
  381. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  382. zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
  383. out:
  384. put_device(&port->sysfs_device);
  385. }
  386. /**
  387. * zfcp_fc_test_link - lightweight link test procedure
  388. * @port: port to be tested
  389. *
  390. * Test status of a link to a remote port using the ELS command ADISC.
  391. * If there is a problem with the remote port, error recovery steps
  392. * will be triggered.
  393. */
  394. void zfcp_fc_test_link(struct zfcp_port *port)
  395. {
  396. get_device(&port->sysfs_device);
  397. if (!queue_work(port->adapter->work_queue, &port->test_link_work))
  398. put_device(&port->sysfs_device);
  399. }
  400. static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
  401. {
  402. struct scatterlist *sg = &gpn_ft->sg_req;
  403. kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
  404. zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
  405. kfree(gpn_ft);
  406. }
  407. static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
  408. {
  409. struct zfcp_fc_gpn_ft *gpn_ft;
  410. struct zfcp_fc_gpn_ft_req *req;
  411. gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
  412. if (!gpn_ft)
  413. return NULL;
  414. req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
  415. if (!req) {
  416. kfree(gpn_ft);
  417. gpn_ft = NULL;
  418. goto out;
  419. }
  420. sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
  421. if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
  422. zfcp_free_sg_env(gpn_ft, buf_num);
  423. gpn_ft = NULL;
  424. }
  425. out:
  426. return gpn_ft;
  427. }
  428. static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
  429. struct zfcp_adapter *adapter, int max_bytes)
  430. {
  431. struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
  432. struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
  433. DECLARE_COMPLETION_ONSTACK(completion);
  434. int ret;
  435. /* prepare CT IU for GPN_FT */
  436. req->ct_hdr.ct_rev = FC_CT_REV;
  437. req->ct_hdr.ct_fs_type = FC_FST_DIR;
  438. req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
  439. req->ct_hdr.ct_options = 0;
  440. req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
  441. req->ct_hdr.ct_mr_size = max_bytes / 4;
  442. req->gpn_ft.fn_domain_id_scope = 0;
  443. req->gpn_ft.fn_area_id_scope = 0;
  444. req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
  445. /* prepare zfcp_send_ct */
  446. ct->handler = zfcp_fc_complete;
  447. ct->handler_data = &completion;
  448. ct->req = &gpn_ft->sg_req;
  449. ct->resp = gpn_ft->sg_resp;
  450. ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct, NULL);
  451. if (!ret)
  452. wait_for_completion(&completion);
  453. return ret;
  454. }
  455. static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
  456. {
  457. if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
  458. return;
  459. atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
  460. if ((port->supported_classes != 0) ||
  461. !list_empty(&port->unit_list))
  462. return;
  463. list_move_tail(&port->list, lh);
  464. }
  465. static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
  466. struct zfcp_adapter *adapter, int max_entries)
  467. {
  468. struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
  469. struct scatterlist *sg = gpn_ft->sg_resp;
  470. struct fc_ct_hdr *hdr = sg_virt(sg);
  471. struct fc_gpn_ft_resp *acc = sg_virt(sg);
  472. struct zfcp_port *port, *tmp;
  473. unsigned long flags;
  474. LIST_HEAD(remove_lh);
  475. u32 d_id;
  476. int ret = 0, x, last = 0;
  477. if (ct->status)
  478. return -EIO;
  479. if (hdr->ct_cmd != FC_FS_ACC) {
  480. if (hdr->ct_reason == FC_BA_RJT_UNABLE)
  481. return -EAGAIN; /* might be a temporary condition */
  482. return -EIO;
  483. }
  484. if (hdr->ct_mr_size) {
  485. dev_warn(&adapter->ccw_device->dev,
  486. "The name server reported %d words residual data\n",
  487. hdr->ct_mr_size);
  488. return -E2BIG;
  489. }
  490. /* first entry is the header */
  491. for (x = 1; x < max_entries && !last; x++) {
  492. if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
  493. acc++;
  494. else
  495. acc = sg_virt(++sg);
  496. last = acc->fp_flags & FC_NS_FID_LAST;
  497. d_id = ntoh24(acc->fp_fid);
  498. /* don't attach ports with a well known address */
  499. if (d_id >= FC_FID_WELL_KNOWN_BASE)
  500. continue;
  501. /* skip the adapter's port and known remote ports */
  502. if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
  503. continue;
  504. port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
  505. ZFCP_STATUS_COMMON_NOESC, d_id);
  506. if (!IS_ERR(port))
  507. zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
  508. else if (PTR_ERR(port) != -EEXIST)
  509. ret = PTR_ERR(port);
  510. }
  511. zfcp_erp_wait(adapter);
  512. write_lock_irqsave(&adapter->port_list_lock, flags);
  513. list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
  514. zfcp_fc_validate_port(port, &remove_lh);
  515. write_unlock_irqrestore(&adapter->port_list_lock, flags);
  516. list_for_each_entry_safe(port, tmp, &remove_lh, list) {
  517. zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
  518. zfcp_device_unregister(&port->sysfs_device,
  519. &zfcp_sysfs_port_attrs);
  520. }
  521. return ret;
  522. }
  523. /**
  524. * zfcp_fc_scan_ports - scan remote ports and attach new ports
  525. * @work: reference to scheduled work
  526. */
  527. void zfcp_fc_scan_ports(struct work_struct *work)
  528. {
  529. struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
  530. scan_work);
  531. int ret, i;
  532. struct zfcp_fc_gpn_ft *gpn_ft;
  533. int chain, max_entries, buf_num, max_bytes;
  534. chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
  535. buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
  536. max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
  537. max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
  538. if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
  539. fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
  540. return;
  541. if (zfcp_fc_wka_port_get(&adapter->gs->ds))
  542. return;
  543. gpn_ft = zfcp_alloc_sg_env(buf_num);
  544. if (!gpn_ft)
  545. goto out;
  546. for (i = 0; i < 3; i++) {
  547. ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
  548. if (!ret) {
  549. ret = zfcp_fc_eval_gpn_ft(gpn_ft, adapter, max_entries);
  550. if (ret == -EAGAIN)
  551. ssleep(1);
  552. else
  553. break;
  554. }
  555. }
  556. zfcp_free_sg_env(gpn_ft, buf_num);
  557. out:
  558. zfcp_fc_wka_port_put(&adapter->gs->ds);
  559. }
  560. static void zfcp_fc_ct_els_job_handler(void *data)
  561. {
  562. struct fc_bsg_job *job = data;
  563. struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
  564. int status = zfcp_ct_els->status;
  565. int reply_status;
  566. reply_status = status ? FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
  567. job->reply->reply_data.ctels_reply.status = reply_status;
  568. job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
  569. job->job_done(job);
  570. }
  571. static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
  572. struct zfcp_adapter *adapter)
  573. {
  574. struct zfcp_fsf_ct_els *els = job->dd_data;
  575. struct fc_rport *rport = job->rport;
  576. struct zfcp_port *port;
  577. u32 d_id;
  578. if (rport) {
  579. port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
  580. if (!port)
  581. return -EINVAL;
  582. d_id = port->d_id;
  583. put_device(&port->sysfs_device);
  584. } else
  585. d_id = ntoh24(job->request->rqst_data.h_els.port_id);
  586. return zfcp_fsf_send_els(adapter, d_id, els);
  587. }
  588. static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
  589. struct zfcp_adapter *adapter)
  590. {
  591. int ret;
  592. u8 gs_type;
  593. struct zfcp_fsf_ct_els *ct = job->dd_data;
  594. struct zfcp_fc_wka_port *wka_port;
  595. u32 preamble_word1;
  596. preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
  597. gs_type = (preamble_word1 & 0xff000000) >> 24;
  598. switch (gs_type) {
  599. case FC_FST_ALIAS:
  600. wka_port = &adapter->gs->as;
  601. break;
  602. case FC_FST_MGMT:
  603. wka_port = &adapter->gs->ms;
  604. break;
  605. case FC_FST_TIME:
  606. wka_port = &adapter->gs->ts;
  607. break;
  608. case FC_FST_DIR:
  609. wka_port = &adapter->gs->ds;
  610. break;
  611. default:
  612. return -EINVAL; /* no such service */
  613. }
  614. ret = zfcp_fc_wka_port_get(wka_port);
  615. if (ret)
  616. return ret;
  617. ret = zfcp_fsf_send_ct(wka_port, ct, NULL);
  618. if (ret)
  619. zfcp_fc_wka_port_put(wka_port);
  620. return ret;
  621. }
  622. int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
  623. {
  624. struct Scsi_Host *shost;
  625. struct zfcp_adapter *adapter;
  626. struct zfcp_fsf_ct_els *ct_els = job->dd_data;
  627. shost = job->rport ? rport_to_shost(job->rport) : job->shost;
  628. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  629. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
  630. return -EINVAL;
  631. ct_els->req = job->request_payload.sg_list;
  632. ct_els->resp = job->reply_payload.sg_list;
  633. ct_els->handler = zfcp_fc_ct_els_job_handler;
  634. ct_els->handler_data = job;
  635. switch (job->request->msgcode) {
  636. case FC_BSG_RPT_ELS:
  637. case FC_BSG_HST_ELS_NOLOGIN:
  638. return zfcp_fc_exec_els_job(job, adapter);
  639. case FC_BSG_RPT_CT:
  640. case FC_BSG_HST_CT:
  641. return zfcp_fc_exec_ct_job(job, adapter);
  642. default:
  643. return -EINVAL;
  644. }
  645. }
  646. int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
  647. {
  648. struct zfcp_fc_wka_ports *wka_ports;
  649. wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
  650. if (!wka_ports)
  651. return -ENOMEM;
  652. adapter->gs = wka_ports;
  653. zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
  654. zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
  655. zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
  656. zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
  657. return 0;
  658. }
  659. void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
  660. {
  661. kfree(adapter->gs);
  662. adapter->gs = NULL;
  663. }