zfcp_fc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 <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. ZFCP_FC_CTELS_TMO);
  224. if (!ret) {
  225. wait_for_completion(&completion);
  226. zfcp_fc_ns_gid_pn_eval(gid_pn);
  227. }
  228. return ret;
  229. }
  230. /**
  231. * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
  232. * @port: port where GID_PN request is needed
  233. * return: -ENOMEM on error, 0 otherwise
  234. */
  235. static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
  236. {
  237. int ret;
  238. struct zfcp_fc_gid_pn *gid_pn;
  239. struct zfcp_adapter *adapter = port->adapter;
  240. gid_pn = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
  241. if (!gid_pn)
  242. return -ENOMEM;
  243. memset(gid_pn, 0, sizeof(*gid_pn));
  244. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  245. if (ret)
  246. goto out;
  247. ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
  248. zfcp_fc_wka_port_put(&adapter->gs->ds);
  249. out:
  250. mempool_free(gid_pn, adapter->pool.gid_pn);
  251. return ret;
  252. }
  253. void zfcp_fc_port_did_lookup(struct work_struct *work)
  254. {
  255. int ret;
  256. struct zfcp_port *port = container_of(work, struct zfcp_port,
  257. gid_pn_work);
  258. ret = zfcp_fc_ns_gid_pn(port);
  259. if (ret) {
  260. /* could not issue gid_pn for some reason */
  261. zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
  262. goto out;
  263. }
  264. if (!port->d_id) {
  265. zfcp_erp_port_failed(port, "fcgpn_2", NULL);
  266. goto out;
  267. }
  268. zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
  269. out:
  270. put_device(&port->dev);
  271. }
  272. /**
  273. * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
  274. * @port: The zfcp_port to lookup the d_id for.
  275. */
  276. void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
  277. {
  278. get_device(&port->dev);
  279. if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
  280. put_device(&port->dev);
  281. }
  282. /**
  283. * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
  284. * @port: zfcp_port structure
  285. * @plogi: plogi payload
  286. *
  287. * Evaluate PLOGI playload and copy important fields into zfcp_port structure
  288. */
  289. void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
  290. {
  291. if (plogi->fl_wwpn != port->wwpn) {
  292. port->d_id = 0;
  293. dev_warn(&port->adapter->ccw_device->dev,
  294. "A port opened with WWPN 0x%016Lx returned data that "
  295. "identifies it as WWPN 0x%016Lx\n",
  296. (unsigned long long) port->wwpn,
  297. (unsigned long long) plogi->fl_wwpn);
  298. return;
  299. }
  300. port->wwnn = plogi->fl_wwnn;
  301. port->maxframe_size = plogi->fl_csp.sp_bb_data;
  302. if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
  303. port->supported_classes |= FC_COS_CLASS1;
  304. if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
  305. port->supported_classes |= FC_COS_CLASS2;
  306. if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
  307. port->supported_classes |= FC_COS_CLASS3;
  308. if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
  309. port->supported_classes |= FC_COS_CLASS4;
  310. }
  311. static void zfcp_fc_adisc_handler(void *data)
  312. {
  313. struct zfcp_fc_els_adisc *adisc = data;
  314. struct zfcp_port *port = adisc->els.port;
  315. struct fc_els_adisc *adisc_resp = &adisc->adisc_resp;
  316. if (adisc->els.status) {
  317. /* request rejected or timed out */
  318. zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  319. "fcadh_1", NULL);
  320. goto out;
  321. }
  322. if (!port->wwnn)
  323. port->wwnn = adisc_resp->adisc_wwnn;
  324. if ((port->wwpn != adisc_resp->adisc_wwpn) ||
  325. !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
  326. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  327. "fcadh_2", NULL);
  328. goto out;
  329. }
  330. /* port is good, unblock rport without going through erp */
  331. zfcp_scsi_schedule_rport_register(port);
  332. out:
  333. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  334. put_device(&port->dev);
  335. kmem_cache_free(zfcp_data.adisc_cache, adisc);
  336. }
  337. static int zfcp_fc_adisc(struct zfcp_port *port)
  338. {
  339. struct zfcp_fc_els_adisc *adisc;
  340. struct zfcp_adapter *adapter = port->adapter;
  341. int ret;
  342. adisc = kmem_cache_alloc(zfcp_data.adisc_cache, GFP_ATOMIC);
  343. if (!adisc)
  344. return -ENOMEM;
  345. adisc->els.port = port;
  346. adisc->els.req = &adisc->req;
  347. adisc->els.resp = &adisc->resp;
  348. sg_init_one(adisc->els.req, &adisc->adisc_req,
  349. sizeof(struct fc_els_adisc));
  350. sg_init_one(adisc->els.resp, &adisc->adisc_resp,
  351. sizeof(struct fc_els_adisc));
  352. adisc->els.handler = zfcp_fc_adisc_handler;
  353. adisc->els.handler_data = adisc;
  354. /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
  355. without FC-AL-2 capability, so we don't set it */
  356. adisc->adisc_req.adisc_wwpn = fc_host_port_name(adapter->scsi_host);
  357. adisc->adisc_req.adisc_wwnn = fc_host_node_name(adapter->scsi_host);
  358. adisc->adisc_req.adisc_cmd = ELS_ADISC;
  359. hton24(adisc->adisc_req.adisc_port_id,
  360. fc_host_port_id(adapter->scsi_host));
  361. ret = zfcp_fsf_send_els(adapter, port->d_id, &adisc->els,
  362. ZFCP_FC_CTELS_TMO);
  363. if (ret)
  364. kmem_cache_free(zfcp_data.adisc_cache, adisc);
  365. return ret;
  366. }
  367. void zfcp_fc_link_test_work(struct work_struct *work)
  368. {
  369. struct zfcp_port *port =
  370. container_of(work, struct zfcp_port, test_link_work);
  371. int retval;
  372. get_device(&port->dev);
  373. port->rport_task = RPORT_DEL;
  374. zfcp_scsi_rport_work(&port->rport_work);
  375. /* only issue one test command at one time per port */
  376. if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
  377. goto out;
  378. atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  379. retval = zfcp_fc_adisc(port);
  380. if (retval == 0)
  381. return;
  382. /* send of ADISC was not possible */
  383. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  384. zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
  385. out:
  386. put_device(&port->dev);
  387. }
  388. /**
  389. * zfcp_fc_test_link - lightweight link test procedure
  390. * @port: port to be tested
  391. *
  392. * Test status of a link to a remote port using the ELS command ADISC.
  393. * If there is a problem with the remote port, error recovery steps
  394. * will be triggered.
  395. */
  396. void zfcp_fc_test_link(struct zfcp_port *port)
  397. {
  398. get_device(&port->dev);
  399. if (!queue_work(port->adapter->work_queue, &port->test_link_work))
  400. put_device(&port->dev);
  401. }
  402. static void zfcp_free_sg_env(struct zfcp_fc_gpn_ft *gpn_ft, int buf_num)
  403. {
  404. struct scatterlist *sg = &gpn_ft->sg_req;
  405. kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
  406. zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
  407. kfree(gpn_ft);
  408. }
  409. static struct zfcp_fc_gpn_ft *zfcp_alloc_sg_env(int buf_num)
  410. {
  411. struct zfcp_fc_gpn_ft *gpn_ft;
  412. struct zfcp_fc_gpn_ft_req *req;
  413. gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
  414. if (!gpn_ft)
  415. return NULL;
  416. req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
  417. if (!req) {
  418. kfree(gpn_ft);
  419. gpn_ft = NULL;
  420. goto out;
  421. }
  422. sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
  423. if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
  424. zfcp_free_sg_env(gpn_ft, buf_num);
  425. gpn_ft = NULL;
  426. }
  427. out:
  428. return gpn_ft;
  429. }
  430. static int zfcp_fc_send_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
  431. struct zfcp_adapter *adapter, int max_bytes)
  432. {
  433. struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
  434. struct zfcp_fc_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
  435. DECLARE_COMPLETION_ONSTACK(completion);
  436. int ret;
  437. /* prepare CT IU for GPN_FT */
  438. req->ct_hdr.ct_rev = FC_CT_REV;
  439. req->ct_hdr.ct_fs_type = FC_FST_DIR;
  440. req->ct_hdr.ct_fs_subtype = FC_NS_SUBTYPE;
  441. req->ct_hdr.ct_options = 0;
  442. req->ct_hdr.ct_cmd = FC_NS_GPN_FT;
  443. req->ct_hdr.ct_mr_size = max_bytes / 4;
  444. req->gpn_ft.fn_domain_id_scope = 0;
  445. req->gpn_ft.fn_area_id_scope = 0;
  446. req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
  447. /* prepare zfcp_send_ct */
  448. ct->handler = zfcp_fc_complete;
  449. ct->handler_data = &completion;
  450. ct->req = &gpn_ft->sg_req;
  451. ct->resp = gpn_ft->sg_resp;
  452. ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct, NULL,
  453. ZFCP_FC_CTELS_TMO);
  454. if (!ret)
  455. wait_for_completion(&completion);
  456. return ret;
  457. }
  458. static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
  459. {
  460. if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
  461. return;
  462. atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
  463. if ((port->supported_classes != 0) ||
  464. !list_empty(&port->unit_list))
  465. return;
  466. list_move_tail(&port->list, lh);
  467. }
  468. static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_gpn_ft *gpn_ft,
  469. struct zfcp_adapter *adapter, int max_entries)
  470. {
  471. struct zfcp_fsf_ct_els *ct = &gpn_ft->ct;
  472. struct scatterlist *sg = gpn_ft->sg_resp;
  473. struct fc_ct_hdr *hdr = sg_virt(sg);
  474. struct fc_gpn_ft_resp *acc = sg_virt(sg);
  475. struct zfcp_port *port, *tmp;
  476. unsigned long flags;
  477. LIST_HEAD(remove_lh);
  478. u32 d_id;
  479. int ret = 0, x, last = 0;
  480. if (ct->status)
  481. return -EIO;
  482. if (hdr->ct_cmd != FC_FS_ACC) {
  483. if (hdr->ct_reason == FC_BA_RJT_UNABLE)
  484. return -EAGAIN; /* might be a temporary condition */
  485. return -EIO;
  486. }
  487. if (hdr->ct_mr_size) {
  488. dev_warn(&adapter->ccw_device->dev,
  489. "The name server reported %d words residual data\n",
  490. hdr->ct_mr_size);
  491. return -E2BIG;
  492. }
  493. /* first entry is the header */
  494. for (x = 1; x < max_entries && !last; x++) {
  495. if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
  496. acc++;
  497. else
  498. acc = sg_virt(++sg);
  499. last = acc->fp_flags & FC_NS_FID_LAST;
  500. d_id = ntoh24(acc->fp_fid);
  501. /* don't attach ports with a well known address */
  502. if (d_id >= FC_FID_WELL_KNOWN_BASE)
  503. continue;
  504. /* skip the adapter's port and known remote ports */
  505. if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
  506. continue;
  507. port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
  508. ZFCP_STATUS_COMMON_NOESC, d_id);
  509. if (!IS_ERR(port))
  510. zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
  511. else if (PTR_ERR(port) != -EEXIST)
  512. ret = PTR_ERR(port);
  513. }
  514. zfcp_erp_wait(adapter);
  515. write_lock_irqsave(&adapter->port_list_lock, flags);
  516. list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
  517. zfcp_fc_validate_port(port, &remove_lh);
  518. write_unlock_irqrestore(&adapter->port_list_lock, flags);
  519. list_for_each_entry_safe(port, tmp, &remove_lh, list) {
  520. zfcp_erp_port_shutdown(port, 0, "fcegpf2", NULL);
  521. zfcp_device_unregister(&port->dev, &zfcp_sysfs_port_attrs);
  522. }
  523. return ret;
  524. }
  525. /**
  526. * zfcp_fc_scan_ports - scan remote ports and attach new ports
  527. * @work: reference to scheduled work
  528. */
  529. void zfcp_fc_scan_ports(struct work_struct *work)
  530. {
  531. struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
  532. scan_work);
  533. int ret, i;
  534. struct zfcp_fc_gpn_ft *gpn_ft;
  535. int chain, max_entries, buf_num, max_bytes;
  536. chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
  537. buf_num = chain ? ZFCP_FC_GPN_FT_NUM_BUFS : 1;
  538. max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
  539. max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_PAGE;
  540. if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
  541. fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
  542. return;
  543. if (zfcp_fc_wka_port_get(&adapter->gs->ds))
  544. return;
  545. gpn_ft = zfcp_alloc_sg_env(buf_num);
  546. if (!gpn_ft)
  547. goto out;
  548. for (i = 0; i < 3; i++) {
  549. ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
  550. if (!ret) {
  551. ret = zfcp_fc_eval_gpn_ft(gpn_ft, adapter, max_entries);
  552. if (ret == -EAGAIN)
  553. ssleep(1);
  554. else
  555. break;
  556. }
  557. }
  558. zfcp_free_sg_env(gpn_ft, buf_num);
  559. out:
  560. zfcp_fc_wka_port_put(&adapter->gs->ds);
  561. }
  562. static void zfcp_fc_ct_els_job_handler(void *data)
  563. {
  564. struct fc_bsg_job *job = data;
  565. struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
  566. struct fc_bsg_reply *jr = job->reply;
  567. jr->reply_payload_rcv_len = job->reply_payload.payload_len;
  568. jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
  569. jr->result = zfcp_ct_els->status ? -EIO : 0;
  570. job->job_done(job);
  571. }
  572. static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct fc_bsg_job *job)
  573. {
  574. u32 preamble_word1;
  575. u8 gs_type;
  576. struct zfcp_adapter *adapter;
  577. preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
  578. gs_type = (preamble_word1 & 0xff000000) >> 24;
  579. adapter = (struct zfcp_adapter *) job->shost->hostdata[0];
  580. switch (gs_type) {
  581. case FC_FST_ALIAS:
  582. return &adapter->gs->as;
  583. case FC_FST_MGMT:
  584. return &adapter->gs->ms;
  585. case FC_FST_TIME:
  586. return &adapter->gs->ts;
  587. break;
  588. case FC_FST_DIR:
  589. return &adapter->gs->ds;
  590. break;
  591. default:
  592. return NULL;
  593. }
  594. }
  595. static void zfcp_fc_ct_job_handler(void *data)
  596. {
  597. struct fc_bsg_job *job = data;
  598. struct zfcp_fc_wka_port *wka_port;
  599. wka_port = zfcp_fc_job_wka_port(job);
  600. zfcp_fc_wka_port_put(wka_port);
  601. zfcp_fc_ct_els_job_handler(data);
  602. }
  603. static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
  604. struct zfcp_adapter *adapter)
  605. {
  606. struct zfcp_fsf_ct_els *els = job->dd_data;
  607. struct fc_rport *rport = job->rport;
  608. struct zfcp_port *port;
  609. u32 d_id;
  610. if (rport) {
  611. port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
  612. if (!port)
  613. return -EINVAL;
  614. d_id = port->d_id;
  615. put_device(&port->dev);
  616. } else
  617. d_id = ntoh24(job->request->rqst_data.h_els.port_id);
  618. els->handler = zfcp_fc_ct_els_job_handler;
  619. return zfcp_fsf_send_els(adapter, d_id, els, job->req->timeout / HZ);
  620. }
  621. static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
  622. struct zfcp_adapter *adapter)
  623. {
  624. int ret;
  625. struct zfcp_fsf_ct_els *ct = job->dd_data;
  626. struct zfcp_fc_wka_port *wka_port;
  627. wka_port = zfcp_fc_job_wka_port(job);
  628. if (!wka_port)
  629. return -EINVAL;
  630. ret = zfcp_fc_wka_port_get(wka_port);
  631. if (ret)
  632. return ret;
  633. ct->handler = zfcp_fc_ct_job_handler;
  634. ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->req->timeout / HZ);
  635. if (ret)
  636. zfcp_fc_wka_port_put(wka_port);
  637. return ret;
  638. }
  639. int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
  640. {
  641. struct Scsi_Host *shost;
  642. struct zfcp_adapter *adapter;
  643. struct zfcp_fsf_ct_els *ct_els = job->dd_data;
  644. shost = job->rport ? rport_to_shost(job->rport) : job->shost;
  645. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  646. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
  647. return -EINVAL;
  648. ct_els->req = job->request_payload.sg_list;
  649. ct_els->resp = job->reply_payload.sg_list;
  650. ct_els->handler_data = job;
  651. switch (job->request->msgcode) {
  652. case FC_BSG_RPT_ELS:
  653. case FC_BSG_HST_ELS_NOLOGIN:
  654. return zfcp_fc_exec_els_job(job, adapter);
  655. case FC_BSG_RPT_CT:
  656. case FC_BSG_HST_CT:
  657. return zfcp_fc_exec_ct_job(job, adapter);
  658. default:
  659. return -EINVAL;
  660. }
  661. }
  662. int zfcp_fc_timeout_bsg_job(struct fc_bsg_job *job)
  663. {
  664. /* hardware tracks timeout, reset bsg timeout to not interfere */
  665. return -EAGAIN;
  666. }
  667. int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
  668. {
  669. struct zfcp_fc_wka_ports *wka_ports;
  670. wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
  671. if (!wka_ports)
  672. return -ENOMEM;
  673. adapter->gs = wka_ports;
  674. zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
  675. zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
  676. zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
  677. zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
  678. return 0;
  679. }
  680. void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
  681. {
  682. kfree(adapter->gs);
  683. adapter->gs = NULL;
  684. }