zfcp_fc.c 17 KB

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