zfcp_fc.c 17 KB

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