zfcp_fc.c 22 KB

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