zfcp_fc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 "zfcp_ext.h"
  11. enum rscn_address_format {
  12. RSCN_PORT_ADDRESS = 0x0,
  13. RSCN_AREA_ADDRESS = 0x1,
  14. RSCN_DOMAIN_ADDRESS = 0x2,
  15. RSCN_FABRIC_ADDRESS = 0x3,
  16. };
  17. static u32 rscn_range_mask[] = {
  18. [RSCN_PORT_ADDRESS] = 0xFFFFFF,
  19. [RSCN_AREA_ADDRESS] = 0xFFFF00,
  20. [RSCN_DOMAIN_ADDRESS] = 0xFF0000,
  21. [RSCN_FABRIC_ADDRESS] = 0x000000,
  22. };
  23. struct ct_iu_gpn_ft_req {
  24. struct ct_hdr header;
  25. u8 flags;
  26. u8 domain_id_scope;
  27. u8 area_id_scope;
  28. u8 fc4_type;
  29. } __attribute__ ((packed));
  30. struct gpn_ft_resp_acc {
  31. u8 control;
  32. u8 port_id[3];
  33. u8 reserved[4];
  34. u64 wwpn;
  35. } __attribute__ ((packed));
  36. #define ZFCP_CT_SIZE_ONE_PAGE (PAGE_SIZE - sizeof(struct ct_hdr))
  37. #define ZFCP_GPN_FT_ENTRIES (ZFCP_CT_SIZE_ONE_PAGE \
  38. / sizeof(struct gpn_ft_resp_acc))
  39. #define ZFCP_GPN_FT_BUFFERS 4
  40. #define ZFCP_GPN_FT_MAX_SIZE (ZFCP_GPN_FT_BUFFERS * PAGE_SIZE \
  41. - sizeof(struct ct_hdr))
  42. #define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
  43. struct ct_iu_gpn_ft_resp {
  44. struct ct_hdr header;
  45. struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
  46. } __attribute__ ((packed));
  47. struct zfcp_gpn_ft {
  48. struct zfcp_send_ct ct;
  49. struct scatterlist sg_req;
  50. struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
  51. };
  52. struct zfcp_fc_ns_handler_data {
  53. struct completion done;
  54. void (*handler)(unsigned long);
  55. unsigned long handler_data;
  56. };
  57. static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
  58. {
  59. if (mutex_lock_interruptible(&wka_port->mutex))
  60. return -ERESTARTSYS;
  61. if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
  62. wka_port->status == ZFCP_WKA_PORT_CLOSING) {
  63. wka_port->status = ZFCP_WKA_PORT_OPENING;
  64. if (zfcp_fsf_open_wka_port(wka_port))
  65. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  66. }
  67. mutex_unlock(&wka_port->mutex);
  68. wait_event_timeout(
  69. wka_port->completion_wq,
  70. wka_port->status == ZFCP_WKA_PORT_ONLINE ||
  71. wka_port->status == ZFCP_WKA_PORT_OFFLINE,
  72. HZ >> 1);
  73. if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
  74. atomic_inc(&wka_port->refcount);
  75. return 0;
  76. }
  77. return -EIO;
  78. }
  79. static void zfcp_wka_port_offline(struct work_struct *work)
  80. {
  81. struct delayed_work *dw = to_delayed_work(work);
  82. struct zfcp_wka_port *wka_port =
  83. container_of(dw, struct zfcp_wka_port, work);
  84. mutex_lock(&wka_port->mutex);
  85. if ((atomic_read(&wka_port->refcount) != 0) ||
  86. (wka_port->status != ZFCP_WKA_PORT_ONLINE))
  87. goto out;
  88. wka_port->status = ZFCP_WKA_PORT_CLOSING;
  89. if (zfcp_fsf_close_wka_port(wka_port)) {
  90. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  91. wake_up(&wka_port->completion_wq);
  92. }
  93. out:
  94. mutex_unlock(&wka_port->mutex);
  95. }
  96. static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
  97. {
  98. if (atomic_dec_return(&wka_port->refcount) != 0)
  99. return;
  100. /* wait 10 miliseconds, other reqs might pop in */
  101. schedule_delayed_work(&wka_port->work, HZ / 100);
  102. }
  103. void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
  104. {
  105. struct zfcp_wka_port *wka_port = &adapter->nsp;
  106. init_waitqueue_head(&wka_port->completion_wq);
  107. wka_port->adapter = adapter;
  108. wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
  109. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  110. atomic_set(&wka_port->refcount, 0);
  111. mutex_init(&wka_port->mutex);
  112. INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
  113. }
  114. void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
  115. {
  116. cancel_delayed_work_sync(&wka->work);
  117. mutex_lock(&wka->mutex);
  118. wka->status = ZFCP_WKA_PORT_OFFLINE;
  119. mutex_unlock(&wka->mutex);
  120. }
  121. static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
  122. struct fcp_rscn_element *elem)
  123. {
  124. unsigned long flags;
  125. struct zfcp_port *port;
  126. read_lock_irqsave(&zfcp_data.config_lock, flags);
  127. list_for_each_entry(port, &fsf_req->adapter->port_list_head, list)
  128. if ((port->d_id & range) == (elem->nport_did & range))
  129. zfcp_test_link(port);
  130. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  131. }
  132. static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
  133. {
  134. struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
  135. struct fcp_rscn_head *fcp_rscn_head;
  136. struct fcp_rscn_element *fcp_rscn_element;
  137. u16 i;
  138. u16 no_entries;
  139. u32 range_mask;
  140. fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
  141. fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
  142. /* see FC-FS */
  143. no_entries = fcp_rscn_head->payload_len /
  144. sizeof(struct fcp_rscn_element);
  145. for (i = 1; i < no_entries; i++) {
  146. /* skip head and start with 1st element */
  147. fcp_rscn_element++;
  148. range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
  149. _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
  150. }
  151. schedule_work(&fsf_req->adapter->scan_work);
  152. }
  153. static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
  154. {
  155. struct zfcp_adapter *adapter = req->adapter;
  156. struct zfcp_port *port;
  157. unsigned long flags;
  158. read_lock_irqsave(&zfcp_data.config_lock, flags);
  159. list_for_each_entry(port, &adapter->port_list_head, list)
  160. if (port->wwpn == wwpn)
  161. break;
  162. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  163. if (port && (port->wwpn == wwpn))
  164. zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
  165. }
  166. static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
  167. {
  168. struct fsf_status_read_buffer *status_buffer =
  169. (struct fsf_status_read_buffer *)req->data;
  170. struct fsf_plogi *els_plogi =
  171. (struct fsf_plogi *) status_buffer->payload.data;
  172. zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
  173. }
  174. static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
  175. {
  176. struct fsf_status_read_buffer *status_buffer =
  177. (struct fsf_status_read_buffer *)req->data;
  178. struct fcp_logo *els_logo =
  179. (struct fcp_logo *) status_buffer->payload.data;
  180. zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
  181. }
  182. /**
  183. * zfcp_fc_incoming_els - handle incoming ELS
  184. * @fsf_req - request which contains incoming ELS
  185. */
  186. void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
  187. {
  188. struct fsf_status_read_buffer *status_buffer =
  189. (struct fsf_status_read_buffer *) fsf_req->data;
  190. unsigned int els_type = status_buffer->payload.data[0];
  191. zfcp_san_dbf_event_incoming_els(fsf_req);
  192. if (els_type == LS_PLOGI)
  193. zfcp_fc_incoming_plogi(fsf_req);
  194. else if (els_type == LS_LOGO)
  195. zfcp_fc_incoming_logo(fsf_req);
  196. else if (els_type == LS_RSCN)
  197. zfcp_fc_incoming_rscn(fsf_req);
  198. }
  199. static void zfcp_fc_ns_handler(unsigned long data)
  200. {
  201. struct zfcp_fc_ns_handler_data *compl_rec =
  202. (struct zfcp_fc_ns_handler_data *) data;
  203. if (compl_rec->handler)
  204. compl_rec->handler(compl_rec->handler_data);
  205. complete(&compl_rec->done);
  206. }
  207. static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
  208. {
  209. struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
  210. struct zfcp_send_ct *ct = &gid_pn->ct;
  211. struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
  212. struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
  213. struct zfcp_port *port = gid_pn->port;
  214. if (ct->status)
  215. return;
  216. if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
  217. return;
  218. /* paranoia */
  219. if (ct_iu_req->wwpn != port->wwpn)
  220. return;
  221. /* looks like a valid d_id */
  222. port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
  223. }
  224. int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
  225. struct zfcp_gid_pn_data *gid_pn)
  226. {
  227. struct zfcp_adapter *adapter = erp_action->adapter;
  228. struct zfcp_fc_ns_handler_data compl_rec;
  229. int ret;
  230. /* setup parameters for send generic command */
  231. gid_pn->port = erp_action->port;
  232. gid_pn->ct.wka_port = &adapter->nsp;
  233. gid_pn->ct.handler = zfcp_fc_ns_handler;
  234. gid_pn->ct.handler_data = (unsigned long) &compl_rec;
  235. gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
  236. gid_pn->ct.req = &gid_pn->req;
  237. gid_pn->ct.resp = &gid_pn->resp;
  238. sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
  239. sizeof(struct ct_iu_gid_pn_req));
  240. sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
  241. sizeof(struct ct_iu_gid_pn_resp));
  242. /* setup nameserver request */
  243. gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
  244. gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  245. gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
  246. gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
  247. gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
  248. gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
  249. gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
  250. init_completion(&compl_rec.done);
  251. compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
  252. compl_rec.handler_data = (unsigned long) gid_pn;
  253. ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
  254. erp_action);
  255. if (!ret)
  256. wait_for_completion(&compl_rec.done);
  257. return ret;
  258. }
  259. /**
  260. * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
  261. * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
  262. * return: -ENOMEM on error, 0 otherwise
  263. */
  264. int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
  265. {
  266. int ret;
  267. struct zfcp_gid_pn_data *gid_pn;
  268. struct zfcp_adapter *adapter = erp_action->adapter;
  269. gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
  270. if (!gid_pn)
  271. return -ENOMEM;
  272. memset(gid_pn, 0, sizeof(*gid_pn));
  273. ret = zfcp_wka_port_get(&adapter->nsp);
  274. if (ret)
  275. goto out;
  276. ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
  277. zfcp_wka_port_put(&adapter->nsp);
  278. out:
  279. mempool_free(gid_pn, adapter->pool.data_gid_pn);
  280. return ret;
  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 fsf_plogi *plogi)
  290. {
  291. port->maxframe_size = plogi->serv_param.common_serv_param[7] |
  292. ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
  293. if (plogi->serv_param.class1_serv_param[0] & 0x80)
  294. port->supported_classes |= FC_COS_CLASS1;
  295. if (plogi->serv_param.class2_serv_param[0] & 0x80)
  296. port->supported_classes |= FC_COS_CLASS2;
  297. if (plogi->serv_param.class3_serv_param[0] & 0x80)
  298. port->supported_classes |= FC_COS_CLASS3;
  299. if (plogi->serv_param.class4_serv_param[0] & 0x80)
  300. port->supported_classes |= FC_COS_CLASS4;
  301. }
  302. struct zfcp_els_adisc {
  303. struct zfcp_send_els els;
  304. struct scatterlist req;
  305. struct scatterlist resp;
  306. struct zfcp_ls_adisc ls_adisc;
  307. struct zfcp_ls_adisc ls_adisc_acc;
  308. };
  309. static void zfcp_fc_adisc_handler(unsigned long data)
  310. {
  311. struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
  312. struct zfcp_port *port = adisc->els.port;
  313. struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
  314. if (adisc->els.status) {
  315. /* request rejected or timed out */
  316. zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  317. "fcadh_1", NULL);
  318. goto out;
  319. }
  320. if (!port->wwnn)
  321. port->wwnn = ls_adisc->wwnn;
  322. if ((port->wwpn != ls_adisc->wwpn) ||
  323. !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
  324. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  325. "fcadh_2", NULL);
  326. goto out;
  327. }
  328. /* port is good, unblock rport without going through erp */
  329. zfcp_scsi_schedule_rport_register(port);
  330. out:
  331. zfcp_port_put(port);
  332. kfree(adisc);
  333. }
  334. static int zfcp_fc_adisc(struct zfcp_port *port)
  335. {
  336. struct zfcp_els_adisc *adisc;
  337. struct zfcp_adapter *adapter = port->adapter;
  338. adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
  339. if (!adisc)
  340. return -ENOMEM;
  341. adisc->els.req = &adisc->req;
  342. adisc->els.resp = &adisc->resp;
  343. sg_init_one(adisc->els.req, &adisc->ls_adisc,
  344. sizeof(struct zfcp_ls_adisc));
  345. sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
  346. sizeof(struct zfcp_ls_adisc));
  347. adisc->els.adapter = adapter;
  348. adisc->els.port = port;
  349. adisc->els.d_id = port->d_id;
  350. adisc->els.handler = zfcp_fc_adisc_handler;
  351. adisc->els.handler_data = (unsigned long) adisc;
  352. adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_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->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
  356. adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
  357. adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
  358. return zfcp_fsf_send_els(&adisc->els);
  359. }
  360. void zfcp_fc_link_test_work(struct work_struct *work)
  361. {
  362. struct zfcp_port *port =
  363. container_of(work, struct zfcp_port, test_link_work);
  364. int retval;
  365. zfcp_port_get(port);
  366. port->rport_task = RPORT_DEL;
  367. zfcp_scsi_rport_work(&port->rport_work);
  368. retval = zfcp_fc_adisc(port);
  369. if (retval == 0)
  370. return;
  371. /* send of ADISC was not possible */
  372. zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
  373. zfcp_port_put(port);
  374. }
  375. /**
  376. * zfcp_test_link - lightweight link test procedure
  377. * @port: port to be tested
  378. *
  379. * Test status of a link to a remote port using the ELS command ADISC.
  380. * If there is a problem with the remote port, error recovery steps
  381. * will be triggered.
  382. */
  383. void zfcp_test_link(struct zfcp_port *port)
  384. {
  385. zfcp_port_get(port);
  386. if (!queue_work(zfcp_data.work_queue, &port->test_link_work))
  387. zfcp_port_put(port);
  388. }
  389. static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
  390. {
  391. struct scatterlist *sg = &gpn_ft->sg_req;
  392. kfree(sg_virt(sg)); /* free request buffer */
  393. zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
  394. kfree(gpn_ft);
  395. }
  396. static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
  397. {
  398. struct zfcp_gpn_ft *gpn_ft;
  399. struct ct_iu_gpn_ft_req *req;
  400. gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
  401. if (!gpn_ft)
  402. return NULL;
  403. req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
  404. if (!req) {
  405. kfree(gpn_ft);
  406. gpn_ft = NULL;
  407. goto out;
  408. }
  409. sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
  410. if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
  411. zfcp_free_sg_env(gpn_ft, buf_num);
  412. gpn_ft = NULL;
  413. }
  414. out:
  415. return gpn_ft;
  416. }
  417. static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
  418. struct zfcp_adapter *adapter,
  419. int max_bytes)
  420. {
  421. struct zfcp_send_ct *ct = &gpn_ft->ct;
  422. struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
  423. struct zfcp_fc_ns_handler_data compl_rec;
  424. int ret;
  425. /* prepare CT IU for GPN_FT */
  426. req->header.revision = ZFCP_CT_REVISION;
  427. req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  428. req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
  429. req->header.options = ZFCP_CT_SYNCHRONOUS;
  430. req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
  431. req->header.max_res_size = max_bytes / 4;
  432. req->flags = 0;
  433. req->domain_id_scope = 0;
  434. req->area_id_scope = 0;
  435. req->fc4_type = ZFCP_CT_SCSI_FCP;
  436. /* prepare zfcp_send_ct */
  437. ct->wka_port = &adapter->nsp;
  438. ct->handler = zfcp_fc_ns_handler;
  439. ct->handler_data = (unsigned long)&compl_rec;
  440. ct->timeout = 10;
  441. ct->req = &gpn_ft->sg_req;
  442. ct->resp = gpn_ft->sg_resp;
  443. init_completion(&compl_rec.done);
  444. compl_rec.handler = NULL;
  445. ret = zfcp_fsf_send_ct(ct, NULL, NULL);
  446. if (!ret)
  447. wait_for_completion(&compl_rec.done);
  448. return ret;
  449. }
  450. static void zfcp_validate_port(struct zfcp_port *port)
  451. {
  452. struct zfcp_adapter *adapter = port->adapter;
  453. if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
  454. return;
  455. atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
  456. if ((port->supported_classes != 0) ||
  457. !list_empty(&port->unit_list_head)) {
  458. zfcp_port_put(port);
  459. return;
  460. }
  461. zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
  462. zfcp_erp_wait(adapter);
  463. zfcp_port_put(port);
  464. zfcp_port_dequeue(port);
  465. }
  466. static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
  467. {
  468. struct zfcp_send_ct *ct = &gpn_ft->ct;
  469. struct scatterlist *sg = gpn_ft->sg_resp;
  470. struct ct_hdr *hdr = sg_virt(sg);
  471. struct gpn_ft_resp_acc *acc = sg_virt(sg);
  472. struct zfcp_adapter *adapter = ct->wka_port->adapter;
  473. struct zfcp_port *port, *tmp;
  474. u32 d_id;
  475. int ret = 0, x, last = 0;
  476. if (ct->status)
  477. return -EIO;
  478. if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
  479. if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
  480. return -EAGAIN; /* might be a temporary condition */
  481. return -EIO;
  482. }
  483. if (hdr->max_res_size) {
  484. dev_warn(&adapter->ccw_device->dev,
  485. "The name server reported %d words residual data\n",
  486. hdr->max_res_size);
  487. return -E2BIG;
  488. }
  489. down(&zfcp_data.config_sema);
  490. /* first entry is the header */
  491. for (x = 1; x < max_entries && !last; x++) {
  492. if (x % (ZFCP_GPN_FT_ENTRIES + 1))
  493. acc++;
  494. else
  495. acc = sg_virt(++sg);
  496. last = acc->control & 0x80;
  497. d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
  498. acc->port_id[2];
  499. /* don't attach ports with a well known address */
  500. if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
  501. continue;
  502. /* skip the adapter's port and known remote ports */
  503. if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
  504. continue;
  505. port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
  506. if (port)
  507. continue;
  508. port = zfcp_port_enqueue(adapter, acc->wwpn,
  509. ZFCP_STATUS_COMMON_NOESC, d_id);
  510. if (IS_ERR(port))
  511. ret = PTR_ERR(port);
  512. else
  513. zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
  514. }
  515. zfcp_erp_wait(adapter);
  516. list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
  517. zfcp_validate_port(port);
  518. up(&zfcp_data.config_sema);
  519. return ret;
  520. }
  521. /**
  522. * zfcp_scan_ports - scan remote ports and attach new ports
  523. * @adapter: pointer to struct zfcp_adapter
  524. */
  525. int zfcp_scan_ports(struct zfcp_adapter *adapter)
  526. {
  527. int ret, i;
  528. struct zfcp_gpn_ft *gpn_ft;
  529. int chain, max_entries, buf_num, max_bytes;
  530. chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
  531. buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
  532. max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
  533. max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
  534. if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
  535. fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
  536. return 0;
  537. ret = zfcp_wka_port_get(&adapter->nsp);
  538. if (ret)
  539. return ret;
  540. gpn_ft = zfcp_alloc_sg_env(buf_num);
  541. if (!gpn_ft) {
  542. ret = -ENOMEM;
  543. goto out;
  544. }
  545. for (i = 0; i < 3; i++) {
  546. ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter, max_bytes);
  547. if (!ret) {
  548. ret = zfcp_scan_eval_gpn_ft(gpn_ft, max_entries);
  549. if (ret == -EAGAIN)
  550. ssleep(1);
  551. else
  552. break;
  553. }
  554. }
  555. zfcp_free_sg_env(gpn_ft, buf_num);
  556. out:
  557. zfcp_wka_port_put(&adapter->nsp);
  558. return ret;
  559. }
  560. void _zfcp_scan_ports_later(struct work_struct *work)
  561. {
  562. zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
  563. }