zfcp_fc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * zfcp device driver
  3. *
  4. * Fibre Channel related functions for the zfcp device driver.
  5. *
  6. * Copyright IBM Corporation 2008
  7. */
  8. #include "zfcp_ext.h"
  9. struct ct_iu_gpn_ft_req {
  10. struct ct_hdr header;
  11. u8 flags;
  12. u8 domain_id_scope;
  13. u8 area_id_scope;
  14. u8 fc4_type;
  15. } __attribute__ ((packed));
  16. struct gpn_ft_resp_acc {
  17. u8 control;
  18. u8 port_id[3];
  19. u8 reserved[4];
  20. u64 wwpn;
  21. } __attribute__ ((packed));
  22. #define ZFCP_GPN_FT_ENTRIES ((PAGE_SIZE - sizeof(struct ct_hdr)) \
  23. / sizeof(struct gpn_ft_resp_acc))
  24. #define ZFCP_GPN_FT_BUFFERS 4
  25. #define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
  26. struct ct_iu_gpn_ft_resp {
  27. struct ct_hdr header;
  28. struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
  29. } __attribute__ ((packed));
  30. struct zfcp_gpn_ft {
  31. struct zfcp_send_ct ct;
  32. struct scatterlist sg_req;
  33. struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
  34. };
  35. struct zfcp_fc_ns_handler_data {
  36. struct completion done;
  37. void (*handler)(unsigned long);
  38. unsigned long handler_data;
  39. };
  40. static int zfcp_wka_port_get(struct zfcp_wka_port *wka_port)
  41. {
  42. if (mutex_lock_interruptible(&wka_port->mutex))
  43. return -ERESTARTSYS;
  44. if (wka_port->status != ZFCP_WKA_PORT_ONLINE) {
  45. wka_port->status = ZFCP_WKA_PORT_OPENING;
  46. if (zfcp_fsf_open_wka_port(wka_port))
  47. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  48. }
  49. mutex_unlock(&wka_port->mutex);
  50. wait_event_timeout(
  51. wka_port->completion_wq,
  52. wka_port->status == ZFCP_WKA_PORT_ONLINE ||
  53. wka_port->status == ZFCP_WKA_PORT_OFFLINE,
  54. HZ >> 1);
  55. if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
  56. atomic_inc(&wka_port->refcount);
  57. return 0;
  58. }
  59. return -EIO;
  60. }
  61. static void zfcp_wka_port_offline(struct work_struct *work)
  62. {
  63. struct delayed_work *dw = container_of(work, struct delayed_work, work);
  64. struct zfcp_wka_port *wka_port =
  65. container_of(dw, struct zfcp_wka_port, work);
  66. wait_event(wka_port->completion_wq,
  67. atomic_read(&wka_port->refcount) == 0);
  68. mutex_lock(&wka_port->mutex);
  69. if ((atomic_read(&wka_port->refcount) != 0) ||
  70. (wka_port->status != ZFCP_WKA_PORT_ONLINE))
  71. goto out;
  72. wka_port->status = ZFCP_WKA_PORT_CLOSING;
  73. if (zfcp_fsf_close_wka_port(wka_port)) {
  74. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  75. wake_up(&wka_port->completion_wq);
  76. }
  77. out:
  78. mutex_unlock(&wka_port->mutex);
  79. }
  80. static void zfcp_wka_port_put(struct zfcp_wka_port *wka_port)
  81. {
  82. if (atomic_dec_return(&wka_port->refcount) != 0)
  83. return;
  84. /* wait 10 miliseconds, other reqs might pop in */
  85. schedule_delayed_work(&wka_port->work, HZ / 100);
  86. }
  87. void zfcp_fc_nameserver_init(struct zfcp_adapter *adapter)
  88. {
  89. struct zfcp_wka_port *wka_port = &adapter->nsp;
  90. init_waitqueue_head(&wka_port->completion_wq);
  91. wka_port->adapter = adapter;
  92. wka_port->d_id = ZFCP_DID_DIRECTORY_SERVICE;
  93. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  94. atomic_set(&wka_port->refcount, 0);
  95. mutex_init(&wka_port->mutex);
  96. INIT_DELAYED_WORK(&wka_port->work, zfcp_wka_port_offline);
  97. }
  98. static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
  99. struct fcp_rscn_element *elem)
  100. {
  101. unsigned long flags;
  102. struct zfcp_port *port;
  103. read_lock_irqsave(&zfcp_data.config_lock, flags);
  104. list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
  105. /* FIXME: ZFCP_STATUS_PORT_DID_DID check is racy */
  106. if (!(atomic_read(&port->status) & ZFCP_STATUS_PORT_DID_DID))
  107. /* Try to connect to unused ports anyway. */
  108. zfcp_erp_port_reopen(port,
  109. ZFCP_STATUS_COMMON_ERP_FAILED,
  110. 82, fsf_req);
  111. else if ((port->d_id & range) == (elem->nport_did & range))
  112. /* Check connection status for connected ports */
  113. zfcp_test_link(port);
  114. }
  115. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  116. }
  117. static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
  118. {
  119. struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
  120. struct fcp_rscn_head *fcp_rscn_head;
  121. struct fcp_rscn_element *fcp_rscn_element;
  122. u16 i;
  123. u16 no_entries;
  124. u32 range_mask;
  125. fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
  126. fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
  127. /* see FC-FS */
  128. no_entries = fcp_rscn_head->payload_len /
  129. sizeof(struct fcp_rscn_element);
  130. for (i = 1; i < no_entries; i++) {
  131. /* skip head and start with 1st element */
  132. fcp_rscn_element++;
  133. switch (fcp_rscn_element->addr_format) {
  134. case ZFCP_PORT_ADDRESS:
  135. range_mask = ZFCP_PORTS_RANGE_PORT;
  136. break;
  137. case ZFCP_AREA_ADDRESS:
  138. range_mask = ZFCP_PORTS_RANGE_AREA;
  139. break;
  140. case ZFCP_DOMAIN_ADDRESS:
  141. range_mask = ZFCP_PORTS_RANGE_DOMAIN;
  142. break;
  143. case ZFCP_FABRIC_ADDRESS:
  144. range_mask = ZFCP_PORTS_RANGE_FABRIC;
  145. break;
  146. default:
  147. continue;
  148. }
  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, 83, 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. atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
  218. return;
  219. }
  220. /* paranoia */
  221. if (ct_iu_req->wwpn != port->wwpn)
  222. return;
  223. /* looks like a valid d_id */
  224. port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
  225. atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
  226. }
  227. int static zfcp_fc_ns_gid_pn_request(struct zfcp_erp_action *erp_action,
  228. struct zfcp_gid_pn_data *gid_pn)
  229. {
  230. struct zfcp_adapter *adapter = erp_action->adapter;
  231. struct zfcp_fc_ns_handler_data compl_rec;
  232. int ret;
  233. /* setup parameters for send generic command */
  234. gid_pn->port = erp_action->port;
  235. gid_pn->ct.wka_port = &adapter->nsp;
  236. gid_pn->ct.handler = zfcp_fc_ns_handler;
  237. gid_pn->ct.handler_data = (unsigned long) &compl_rec;
  238. gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
  239. gid_pn->ct.req = &gid_pn->req;
  240. gid_pn->ct.resp = &gid_pn->resp;
  241. gid_pn->ct.req_count = 1;
  242. gid_pn->ct.resp_count = 1;
  243. sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
  244. sizeof(struct ct_iu_gid_pn_req));
  245. sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
  246. sizeof(struct ct_iu_gid_pn_resp));
  247. /* setup nameserver request */
  248. gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
  249. gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  250. gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
  251. gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
  252. gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
  253. gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_MAX_SIZE;
  254. gid_pn->ct_iu_req.wwpn = erp_action->port->wwpn;
  255. init_completion(&compl_rec.done);
  256. compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
  257. compl_rec.handler_data = (unsigned long) gid_pn;
  258. ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
  259. erp_action);
  260. if (!ret)
  261. wait_for_completion(&compl_rec.done);
  262. return ret;
  263. }
  264. /**
  265. * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
  266. * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
  267. * return: -ENOMEM on error, 0 otherwise
  268. */
  269. int zfcp_fc_ns_gid_pn(struct zfcp_erp_action *erp_action)
  270. {
  271. int ret;
  272. struct zfcp_gid_pn_data *gid_pn;
  273. struct zfcp_adapter *adapter = erp_action->adapter;
  274. gid_pn = mempool_alloc(adapter->pool.data_gid_pn, GFP_ATOMIC);
  275. if (!gid_pn)
  276. return -ENOMEM;
  277. memset(gid_pn, 0, sizeof(*gid_pn));
  278. ret = zfcp_wka_port_get(&adapter->nsp);
  279. if (ret)
  280. goto out;
  281. ret = zfcp_fc_ns_gid_pn_request(erp_action, gid_pn);
  282. zfcp_wka_port_put(&adapter->nsp);
  283. out:
  284. mempool_free(gid_pn, adapter->pool.data_gid_pn);
  285. return ret;
  286. }
  287. /**
  288. * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
  289. * @port: zfcp_port structure
  290. * @plogi: plogi payload
  291. *
  292. * Evaluate PLOGI playload and copy important fields into zfcp_port structure
  293. */
  294. void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
  295. {
  296. port->maxframe_size = plogi->serv_param.common_serv_param[7] |
  297. ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
  298. if (plogi->serv_param.class1_serv_param[0] & 0x80)
  299. port->supported_classes |= FC_COS_CLASS1;
  300. if (plogi->serv_param.class2_serv_param[0] & 0x80)
  301. port->supported_classes |= FC_COS_CLASS2;
  302. if (plogi->serv_param.class3_serv_param[0] & 0x80)
  303. port->supported_classes |= FC_COS_CLASS3;
  304. if (plogi->serv_param.class4_serv_param[0] & 0x80)
  305. port->supported_classes |= FC_COS_CLASS4;
  306. }
  307. struct zfcp_els_adisc {
  308. struct zfcp_send_els els;
  309. struct scatterlist req;
  310. struct scatterlist resp;
  311. struct zfcp_ls_adisc ls_adisc;
  312. struct zfcp_ls_adisc ls_adisc_acc;
  313. };
  314. static void zfcp_fc_adisc_handler(unsigned long data)
  315. {
  316. struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
  317. struct zfcp_port *port = adisc->els.port;
  318. struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
  319. if (adisc->els.status) {
  320. /* request rejected or timed out */
  321. zfcp_erp_port_forced_reopen(port, 0, 63, NULL);
  322. goto out;
  323. }
  324. if (!port->wwnn)
  325. port->wwnn = ls_adisc->wwnn;
  326. if (port->wwpn != ls_adisc->wwpn)
  327. zfcp_erp_port_reopen(port, 0, 64, NULL);
  328. out:
  329. zfcp_port_put(port);
  330. kfree(adisc);
  331. }
  332. static int zfcp_fc_adisc(struct zfcp_port *port)
  333. {
  334. struct zfcp_els_adisc *adisc;
  335. struct zfcp_adapter *adapter = port->adapter;
  336. adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
  337. if (!adisc)
  338. return -ENOMEM;
  339. adisc->els.req = &adisc->req;
  340. adisc->els.resp = &adisc->resp;
  341. sg_init_one(adisc->els.req, &adisc->ls_adisc,
  342. sizeof(struct zfcp_ls_adisc));
  343. sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
  344. sizeof(struct zfcp_ls_adisc));
  345. adisc->els.req_count = 1;
  346. adisc->els.resp_count = 1;
  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. /**
  361. * zfcp_test_link - lightweight link test procedure
  362. * @port: port to be tested
  363. *
  364. * Test status of a link to a remote port using the ELS command ADISC.
  365. * If there is a problem with the remote port, error recovery steps
  366. * will be triggered.
  367. */
  368. void zfcp_test_link(struct zfcp_port *port)
  369. {
  370. int retval;
  371. zfcp_port_get(port);
  372. retval = zfcp_fc_adisc(port);
  373. if (retval == 0)
  374. return;
  375. /* send of ADISC was not possible */
  376. zfcp_port_put(port);
  377. if (retval != -EBUSY)
  378. zfcp_erp_port_forced_reopen(port, 0, 65, NULL);
  379. }
  380. static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft)
  381. {
  382. struct scatterlist *sg = &gpn_ft->sg_req;
  383. kfree(sg_virt(sg)); /* free request buffer */
  384. zfcp_sg_free_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS);
  385. kfree(gpn_ft);
  386. }
  387. static struct zfcp_gpn_ft *zfcp_alloc_sg_env(void)
  388. {
  389. struct zfcp_gpn_ft *gpn_ft;
  390. struct ct_iu_gpn_ft_req *req;
  391. gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
  392. if (!gpn_ft)
  393. return NULL;
  394. req = kzalloc(sizeof(struct ct_iu_gpn_ft_req), GFP_KERNEL);
  395. if (!req) {
  396. kfree(gpn_ft);
  397. gpn_ft = NULL;
  398. goto out;
  399. }
  400. sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
  401. if (zfcp_sg_setup_table(gpn_ft->sg_resp, ZFCP_GPN_FT_BUFFERS)) {
  402. zfcp_free_sg_env(gpn_ft);
  403. gpn_ft = NULL;
  404. }
  405. out:
  406. return gpn_ft;
  407. }
  408. static int zfcp_scan_issue_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
  409. struct zfcp_adapter *adapter)
  410. {
  411. struct zfcp_send_ct *ct = &gpn_ft->ct;
  412. struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
  413. struct zfcp_fc_ns_handler_data compl_rec;
  414. int ret;
  415. /* prepare CT IU for GPN_FT */
  416. req->header.revision = ZFCP_CT_REVISION;
  417. req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  418. req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
  419. req->header.options = ZFCP_CT_SYNCHRONOUS;
  420. req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
  421. req->header.max_res_size = (sizeof(struct gpn_ft_resp_acc) *
  422. (ZFCP_GPN_FT_MAX_ENTRIES - 1)) >> 2;
  423. req->flags = 0;
  424. req->domain_id_scope = 0;
  425. req->area_id_scope = 0;
  426. req->fc4_type = ZFCP_CT_SCSI_FCP;
  427. /* prepare zfcp_send_ct */
  428. ct->wka_port = &adapter->nsp;
  429. ct->handler = zfcp_fc_ns_handler;
  430. ct->handler_data = (unsigned long)&compl_rec;
  431. ct->timeout = 10;
  432. ct->req = &gpn_ft->sg_req;
  433. ct->resp = gpn_ft->sg_resp;
  434. ct->req_count = 1;
  435. ct->resp_count = ZFCP_GPN_FT_BUFFERS;
  436. init_completion(&compl_rec.done);
  437. compl_rec.handler = NULL;
  438. ret = zfcp_fsf_send_ct(ct, NULL, NULL);
  439. if (!ret)
  440. wait_for_completion(&compl_rec.done);
  441. return ret;
  442. }
  443. static void zfcp_validate_port(struct zfcp_port *port)
  444. {
  445. struct zfcp_adapter *adapter = port->adapter;
  446. atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
  447. if ((port->supported_classes != 0) ||
  448. !list_empty(&port->unit_list_head)) {
  449. zfcp_port_put(port);
  450. return;
  451. }
  452. zfcp_erp_port_shutdown(port, 0, 151, NULL);
  453. zfcp_erp_wait(adapter);
  454. zfcp_port_put(port);
  455. zfcp_port_dequeue(port);
  456. }
  457. static int zfcp_scan_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft)
  458. {
  459. struct zfcp_send_ct *ct = &gpn_ft->ct;
  460. struct scatterlist *sg = gpn_ft->sg_resp;
  461. struct ct_hdr *hdr = sg_virt(sg);
  462. struct gpn_ft_resp_acc *acc = sg_virt(sg);
  463. struct zfcp_adapter *adapter = ct->wka_port->adapter;
  464. struct zfcp_port *port, *tmp;
  465. u32 d_id;
  466. int ret = 0, x, last = 0;
  467. if (ct->status)
  468. return -EIO;
  469. if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
  470. if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
  471. return -EAGAIN; /* might be a temporary condition */
  472. return -EIO;
  473. }
  474. if (hdr->max_res_size)
  475. return -E2BIG;
  476. down(&zfcp_data.config_sema);
  477. /* first entry is the header */
  478. for (x = 1; x < ZFCP_GPN_FT_MAX_ENTRIES && !last; x++) {
  479. if (x % (ZFCP_GPN_FT_ENTRIES + 1))
  480. acc++;
  481. else
  482. acc = sg_virt(++sg);
  483. last = acc->control & 0x80;
  484. d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
  485. acc->port_id[2];
  486. /* don't attach ports with a well known address */
  487. if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
  488. continue;
  489. /* skip the adapter's port and known remote ports */
  490. if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
  491. continue;
  492. port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
  493. if (port) {
  494. zfcp_port_get(port);
  495. continue;
  496. }
  497. port = zfcp_port_enqueue(adapter, acc->wwpn,
  498. ZFCP_STATUS_PORT_DID_DID |
  499. ZFCP_STATUS_COMMON_NOESC, d_id);
  500. if (IS_ERR(port))
  501. ret = PTR_ERR(port);
  502. else
  503. zfcp_erp_port_reopen(port, 0, 149, NULL);
  504. }
  505. zfcp_erp_wait(adapter);
  506. list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
  507. zfcp_validate_port(port);
  508. up(&zfcp_data.config_sema);
  509. return ret;
  510. }
  511. /**
  512. * zfcp_scan_ports - scan remote ports and attach new ports
  513. * @adapter: pointer to struct zfcp_adapter
  514. */
  515. int zfcp_scan_ports(struct zfcp_adapter *adapter)
  516. {
  517. int ret, i;
  518. struct zfcp_gpn_ft *gpn_ft;
  519. zfcp_erp_wait(adapter); /* wait until adapter is finished with ERP */
  520. if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT)
  521. return 0;
  522. ret = zfcp_wka_port_get(&adapter->nsp);
  523. if (ret)
  524. return ret;
  525. gpn_ft = zfcp_alloc_sg_env();
  526. if (!gpn_ft) {
  527. ret = -ENOMEM;
  528. goto out;
  529. }
  530. for (i = 0; i < 3; i++) {
  531. ret = zfcp_scan_issue_gpn_ft(gpn_ft, adapter);
  532. if (!ret) {
  533. ret = zfcp_scan_eval_gpn_ft(gpn_ft);
  534. if (ret == -EAGAIN)
  535. ssleep(1);
  536. else
  537. break;
  538. }
  539. }
  540. zfcp_free_sg_env(gpn_ft);
  541. out:
  542. zfcp_wka_port_put(&adapter->nsp);
  543. return ret;
  544. }
  545. void _zfcp_scan_ports_later(struct work_struct *work)
  546. {
  547. zfcp_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
  548. }