zfcp_fc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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 gpn_ft_resp_acc {
  24. u8 control;
  25. u8 port_id[3];
  26. u8 reserved[4];
  27. u64 wwpn;
  28. } __attribute__ ((packed));
  29. #define ZFCP_CT_SIZE_ONE_PAGE (PAGE_SIZE - sizeof(struct ct_hdr))
  30. #define ZFCP_GPN_FT_ENTRIES (ZFCP_CT_SIZE_ONE_PAGE \
  31. / sizeof(struct gpn_ft_resp_acc))
  32. #define ZFCP_GPN_FT_BUFFERS 4
  33. #define ZFCP_GPN_FT_MAX_SIZE (ZFCP_GPN_FT_BUFFERS * PAGE_SIZE \
  34. - sizeof(struct ct_hdr))
  35. #define ZFCP_GPN_FT_MAX_ENTRIES ZFCP_GPN_FT_BUFFERS * (ZFCP_GPN_FT_ENTRIES + 1)
  36. struct ct_iu_gpn_ft_resp {
  37. struct ct_hdr header;
  38. struct gpn_ft_resp_acc accept[ZFCP_GPN_FT_ENTRIES];
  39. } __attribute__ ((packed));
  40. struct zfcp_gpn_ft {
  41. struct zfcp_send_ct ct;
  42. struct scatterlist sg_req;
  43. struct scatterlist sg_resp[ZFCP_GPN_FT_BUFFERS];
  44. };
  45. struct zfcp_fc_ns_handler_data {
  46. struct completion done;
  47. void (*handler)(unsigned long);
  48. unsigned long handler_data;
  49. };
  50. static int zfcp_fc_wka_port_get(struct zfcp_wka_port *wka_port)
  51. {
  52. if (mutex_lock_interruptible(&wka_port->mutex))
  53. return -ERESTARTSYS;
  54. if (wka_port->status == ZFCP_WKA_PORT_OFFLINE ||
  55. wka_port->status == ZFCP_WKA_PORT_CLOSING) {
  56. wka_port->status = ZFCP_WKA_PORT_OPENING;
  57. if (zfcp_fsf_open_wka_port(wka_port))
  58. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  59. }
  60. mutex_unlock(&wka_port->mutex);
  61. wait_event(wka_port->completion_wq,
  62. wka_port->status == ZFCP_WKA_PORT_ONLINE ||
  63. wka_port->status == ZFCP_WKA_PORT_OFFLINE);
  64. if (wka_port->status == ZFCP_WKA_PORT_ONLINE) {
  65. atomic_inc(&wka_port->refcount);
  66. return 0;
  67. }
  68. return -EIO;
  69. }
  70. static void zfcp_fc_wka_port_offline(struct work_struct *work)
  71. {
  72. struct delayed_work *dw = to_delayed_work(work);
  73. struct zfcp_wka_port *wka_port =
  74. container_of(dw, struct zfcp_wka_port, work);
  75. mutex_lock(&wka_port->mutex);
  76. if ((atomic_read(&wka_port->refcount) != 0) ||
  77. (wka_port->status != ZFCP_WKA_PORT_ONLINE))
  78. goto out;
  79. wka_port->status = ZFCP_WKA_PORT_CLOSING;
  80. if (zfcp_fsf_close_wka_port(wka_port)) {
  81. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  82. wake_up(&wka_port->completion_wq);
  83. }
  84. out:
  85. mutex_unlock(&wka_port->mutex);
  86. }
  87. static void zfcp_fc_wka_port_put(struct zfcp_wka_port *wka_port)
  88. {
  89. if (atomic_dec_return(&wka_port->refcount) != 0)
  90. return;
  91. /* wait 10 milliseconds, other reqs might pop in */
  92. schedule_delayed_work(&wka_port->work, HZ / 100);
  93. }
  94. static void zfcp_fc_wka_port_init(struct zfcp_wka_port *wka_port, u32 d_id,
  95. struct zfcp_adapter *adapter)
  96. {
  97. init_waitqueue_head(&wka_port->completion_wq);
  98. wka_port->adapter = adapter;
  99. wka_port->d_id = d_id;
  100. wka_port->status = ZFCP_WKA_PORT_OFFLINE;
  101. atomic_set(&wka_port->refcount, 0);
  102. mutex_init(&wka_port->mutex);
  103. INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
  104. }
  105. static void zfcp_fc_wka_port_force_offline(struct zfcp_wka_port *wka)
  106. {
  107. cancel_delayed_work_sync(&wka->work);
  108. mutex_lock(&wka->mutex);
  109. wka->status = ZFCP_WKA_PORT_OFFLINE;
  110. mutex_unlock(&wka->mutex);
  111. }
  112. void zfcp_fc_wka_ports_force_offline(struct zfcp_wka_ports *gs)
  113. {
  114. zfcp_fc_wka_port_force_offline(&gs->ms);
  115. zfcp_fc_wka_port_force_offline(&gs->ts);
  116. zfcp_fc_wka_port_force_offline(&gs->ds);
  117. zfcp_fc_wka_port_force_offline(&gs->as);
  118. zfcp_fc_wka_port_force_offline(&gs->ks);
  119. }
  120. static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
  121. struct fcp_rscn_element *elem)
  122. {
  123. unsigned long flags;
  124. struct zfcp_port *port;
  125. read_lock_irqsave(&zfcp_data.config_lock, flags);
  126. list_for_each_entry(port, &fsf_req->adapter->port_list_head, list) {
  127. if ((port->d_id & range) == (elem->nport_did & range))
  128. zfcp_fc_test_link(port);
  129. if (!port->d_id)
  130. zfcp_erp_port_reopen(port,
  131. ZFCP_STATUS_COMMON_ERP_FAILED,
  132. "fcrscn1", NULL);
  133. }
  134. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  135. }
  136. static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
  137. {
  138. struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
  139. struct fcp_rscn_head *fcp_rscn_head;
  140. struct fcp_rscn_element *fcp_rscn_element;
  141. u16 i;
  142. u16 no_entries;
  143. u32 range_mask;
  144. fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload.data;
  145. fcp_rscn_element = (struct fcp_rscn_element *) fcp_rscn_head;
  146. /* see FC-FS */
  147. no_entries = fcp_rscn_head->payload_len /
  148. sizeof(struct fcp_rscn_element);
  149. for (i = 1; i < no_entries; i++) {
  150. /* skip head and start with 1st element */
  151. fcp_rscn_element++;
  152. range_mask = rscn_range_mask[fcp_rscn_element->addr_format];
  153. _zfcp_fc_incoming_rscn(fsf_req, range_mask, fcp_rscn_element);
  154. }
  155. schedule_work(&fsf_req->adapter->scan_work);
  156. }
  157. static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
  158. {
  159. struct zfcp_adapter *adapter = req->adapter;
  160. struct zfcp_port *port;
  161. unsigned long flags;
  162. read_lock_irqsave(&zfcp_data.config_lock, flags);
  163. list_for_each_entry(port, &adapter->port_list_head, list)
  164. if (port->wwpn == wwpn)
  165. break;
  166. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  167. if (port && (port->wwpn == wwpn))
  168. zfcp_erp_port_forced_reopen(port, 0, "fciwwp1", req);
  169. }
  170. static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
  171. {
  172. struct fsf_status_read_buffer *status_buffer =
  173. (struct fsf_status_read_buffer *)req->data;
  174. struct fsf_plogi *els_plogi =
  175. (struct fsf_plogi *) status_buffer->payload.data;
  176. zfcp_fc_incoming_wwpn(req, els_plogi->serv_param.wwpn);
  177. }
  178. static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
  179. {
  180. struct fsf_status_read_buffer *status_buffer =
  181. (struct fsf_status_read_buffer *)req->data;
  182. struct fcp_logo *els_logo =
  183. (struct fcp_logo *) status_buffer->payload.data;
  184. zfcp_fc_incoming_wwpn(req, els_logo->nport_wwpn);
  185. }
  186. /**
  187. * zfcp_fc_incoming_els - handle incoming ELS
  188. * @fsf_req - request which contains incoming ELS
  189. */
  190. void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
  191. {
  192. struct fsf_status_read_buffer *status_buffer =
  193. (struct fsf_status_read_buffer *) fsf_req->data;
  194. unsigned int els_type = status_buffer->payload.data[0];
  195. zfcp_dbf_san_incoming_els(fsf_req);
  196. if (els_type == LS_PLOGI)
  197. zfcp_fc_incoming_plogi(fsf_req);
  198. else if (els_type == LS_LOGO)
  199. zfcp_fc_incoming_logo(fsf_req);
  200. else if (els_type == LS_RSCN)
  201. zfcp_fc_incoming_rscn(fsf_req);
  202. }
  203. static void zfcp_fc_ns_handler(unsigned long data)
  204. {
  205. struct zfcp_fc_ns_handler_data *compl_rec =
  206. (struct zfcp_fc_ns_handler_data *) data;
  207. if (compl_rec->handler)
  208. compl_rec->handler(compl_rec->handler_data);
  209. complete(&compl_rec->done);
  210. }
  211. static void zfcp_fc_ns_gid_pn_eval(unsigned long data)
  212. {
  213. struct zfcp_gid_pn_data *gid_pn = (struct zfcp_gid_pn_data *) data;
  214. struct zfcp_send_ct *ct = &gid_pn->ct;
  215. struct ct_iu_gid_pn_req *ct_iu_req = sg_virt(ct->req);
  216. struct ct_iu_gid_pn_resp *ct_iu_resp = sg_virt(ct->resp);
  217. struct zfcp_port *port = gid_pn->port;
  218. if (ct->status)
  219. return;
  220. if (ct_iu_resp->header.cmd_rsp_code != ZFCP_CT_ACCEPT)
  221. return;
  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. }
  228. static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
  229. struct zfcp_gid_pn_data *gid_pn)
  230. {
  231. struct zfcp_adapter *adapter = port->adapter;
  232. struct zfcp_fc_ns_handler_data compl_rec;
  233. int ret;
  234. /* setup parameters for send generic command */
  235. gid_pn->port = port;
  236. gid_pn->ct.wka_port = &adapter->gs->ds;
  237. gid_pn->ct.handler = zfcp_fc_ns_handler;
  238. gid_pn->ct.handler_data = (unsigned long) &compl_rec;
  239. gid_pn->ct.req = &gid_pn->req;
  240. gid_pn->ct.resp = &gid_pn->resp;
  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_SIZE_ONE_PAGE / 4;
  252. gid_pn->ct_iu_req.wwpn = 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.gid_pn_req);
  257. if (!ret)
  258. wait_for_completion(&compl_rec.done);
  259. return ret;
  260. }
  261. /**
  262. * zfcp_fc_ns_gid_pn_request - initiate GID_PN nameserver request
  263. * @port: port where GID_PN request is needed
  264. * return: -ENOMEM on error, 0 otherwise
  265. */
  266. static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
  267. {
  268. int ret;
  269. struct zfcp_gid_pn_data *gid_pn;
  270. struct zfcp_adapter *adapter = port->adapter;
  271. gid_pn = mempool_alloc(adapter->pool.gid_pn_data, GFP_ATOMIC);
  272. if (!gid_pn)
  273. return -ENOMEM;
  274. memset(gid_pn, 0, sizeof(*gid_pn));
  275. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  276. if (ret)
  277. goto out;
  278. ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
  279. zfcp_fc_wka_port_put(&adapter->gs->ds);
  280. out:
  281. mempool_free(gid_pn, adapter->pool.gid_pn_data);
  282. return ret;
  283. }
  284. void zfcp_fc_port_did_lookup(struct work_struct *work)
  285. {
  286. int ret;
  287. struct zfcp_port *port = container_of(work, struct zfcp_port,
  288. gid_pn_work);
  289. ret = zfcp_fc_ns_gid_pn(port);
  290. if (ret) {
  291. /* could not issue gid_pn for some reason */
  292. zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
  293. goto out;
  294. }
  295. if (!port->d_id) {
  296. zfcp_erp_port_failed(port, "fcgpn_2", NULL);
  297. goto out;
  298. }
  299. zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
  300. out:
  301. zfcp_port_put(port);
  302. }
  303. /**
  304. * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
  305. * @port: zfcp_port structure
  306. * @plogi: plogi payload
  307. *
  308. * Evaluate PLOGI playload and copy important fields into zfcp_port structure
  309. */
  310. void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
  311. {
  312. port->maxframe_size = plogi->serv_param.common_serv_param[7] |
  313. ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
  314. if (plogi->serv_param.class1_serv_param[0] & 0x80)
  315. port->supported_classes |= FC_COS_CLASS1;
  316. if (plogi->serv_param.class2_serv_param[0] & 0x80)
  317. port->supported_classes |= FC_COS_CLASS2;
  318. if (plogi->serv_param.class3_serv_param[0] & 0x80)
  319. port->supported_classes |= FC_COS_CLASS3;
  320. if (plogi->serv_param.class4_serv_param[0] & 0x80)
  321. port->supported_classes |= FC_COS_CLASS4;
  322. }
  323. struct zfcp_els_adisc {
  324. struct zfcp_send_els els;
  325. struct scatterlist req;
  326. struct scatterlist resp;
  327. struct zfcp_ls_adisc ls_adisc;
  328. struct zfcp_ls_adisc ls_adisc_acc;
  329. };
  330. static void zfcp_fc_adisc_handler(unsigned long data)
  331. {
  332. struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
  333. struct zfcp_port *port = adisc->els.port;
  334. struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
  335. if (adisc->els.status) {
  336. /* request rejected or timed out */
  337. zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  338. "fcadh_1", NULL);
  339. goto out;
  340. }
  341. if (!port->wwnn)
  342. port->wwnn = ls_adisc->wwnn;
  343. if ((port->wwpn != ls_adisc->wwpn) ||
  344. !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
  345. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  346. "fcadh_2", NULL);
  347. goto out;
  348. }
  349. /* port is good, unblock rport without going through erp */
  350. zfcp_scsi_schedule_rport_register(port);
  351. out:
  352. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  353. zfcp_port_put(port);
  354. kfree(adisc);
  355. }
  356. static int zfcp_fc_adisc(struct zfcp_port *port)
  357. {
  358. struct zfcp_els_adisc *adisc;
  359. struct zfcp_adapter *adapter = port->adapter;
  360. adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
  361. if (!adisc)
  362. return -ENOMEM;
  363. adisc->els.req = &adisc->req;
  364. adisc->els.resp = &adisc->resp;
  365. sg_init_one(adisc->els.req, &adisc->ls_adisc,
  366. sizeof(struct zfcp_ls_adisc));
  367. sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
  368. sizeof(struct zfcp_ls_adisc));
  369. adisc->els.adapter = adapter;
  370. adisc->els.port = port;
  371. adisc->els.d_id = port->d_id;
  372. adisc->els.handler = zfcp_fc_adisc_handler;
  373. adisc->els.handler_data = (unsigned long) adisc;
  374. adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
  375. /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
  376. without FC-AL-2 capability, so we don't set it */
  377. adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
  378. adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
  379. adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
  380. return zfcp_fsf_send_els(&adisc->els);
  381. }
  382. void zfcp_fc_link_test_work(struct work_struct *work)
  383. {
  384. struct zfcp_port *port =
  385. container_of(work, struct zfcp_port, test_link_work);
  386. int retval;
  387. zfcp_port_get(port);
  388. port->rport_task = RPORT_DEL;
  389. zfcp_scsi_rport_work(&port->rport_work);
  390. /* only issue one test command at one time per port */
  391. if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
  392. goto out;
  393. atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  394. retval = zfcp_fc_adisc(port);
  395. if (retval == 0)
  396. return;
  397. /* send of ADISC was not possible */
  398. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  399. zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
  400. out:
  401. zfcp_port_put(port);
  402. }
  403. /**
  404. * zfcp_fc_test_link - lightweight link test procedure
  405. * @port: port to be tested
  406. *
  407. * Test status of a link to a remote port using the ELS command ADISC.
  408. * If there is a problem with the remote port, error recovery steps
  409. * will be triggered.
  410. */
  411. void zfcp_fc_test_link(struct zfcp_port *port)
  412. {
  413. zfcp_port_get(port);
  414. if (!queue_work(port->adapter->work_queue, &port->test_link_work))
  415. zfcp_port_put(port);
  416. }
  417. static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
  418. {
  419. struct scatterlist *sg = &gpn_ft->sg_req;
  420. kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
  421. zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
  422. kfree(gpn_ft);
  423. }
  424. static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
  425. {
  426. struct zfcp_gpn_ft *gpn_ft;
  427. struct ct_iu_gpn_ft_req *req;
  428. gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
  429. if (!gpn_ft)
  430. return NULL;
  431. req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
  432. if (!req) {
  433. kfree(gpn_ft);
  434. gpn_ft = NULL;
  435. goto out;
  436. }
  437. sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
  438. if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
  439. zfcp_free_sg_env(gpn_ft, buf_num);
  440. gpn_ft = NULL;
  441. }
  442. out:
  443. return gpn_ft;
  444. }
  445. static int zfcp_fc_send_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
  446. struct zfcp_adapter *adapter, int max_bytes)
  447. {
  448. struct zfcp_send_ct *ct = &gpn_ft->ct;
  449. struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
  450. struct zfcp_fc_ns_handler_data compl_rec;
  451. int ret;
  452. /* prepare CT IU for GPN_FT */
  453. req->header.revision = ZFCP_CT_REVISION;
  454. req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  455. req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
  456. req->header.options = ZFCP_CT_SYNCHRONOUS;
  457. req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
  458. req->header.max_res_size = max_bytes / 4;
  459. req->flags = 0;
  460. req->domain_id_scope = 0;
  461. req->area_id_scope = 0;
  462. req->fc4_type = ZFCP_CT_SCSI_FCP;
  463. /* prepare zfcp_send_ct */
  464. ct->wka_port = &adapter->gs->ds;
  465. ct->handler = zfcp_fc_ns_handler;
  466. ct->handler_data = (unsigned long)&compl_rec;
  467. ct->req = &gpn_ft->sg_req;
  468. ct->resp = gpn_ft->sg_resp;
  469. init_completion(&compl_rec.done);
  470. compl_rec.handler = NULL;
  471. ret = zfcp_fsf_send_ct(ct, NULL);
  472. if (!ret)
  473. wait_for_completion(&compl_rec.done);
  474. return ret;
  475. }
  476. static void zfcp_fc_validate_port(struct zfcp_port *port)
  477. {
  478. struct zfcp_adapter *adapter = port->adapter;
  479. if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
  480. return;
  481. atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
  482. if ((port->supported_classes != 0) ||
  483. !list_empty(&port->unit_list_head)) {
  484. zfcp_port_put(port);
  485. return;
  486. }
  487. zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
  488. zfcp_erp_wait(adapter);
  489. zfcp_port_put(port);
  490. zfcp_port_dequeue(port);
  491. }
  492. static int zfcp_fc_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
  493. {
  494. struct zfcp_send_ct *ct = &gpn_ft->ct;
  495. struct scatterlist *sg = gpn_ft->sg_resp;
  496. struct ct_hdr *hdr = sg_virt(sg);
  497. struct gpn_ft_resp_acc *acc = sg_virt(sg);
  498. struct zfcp_adapter *adapter = ct->wka_port->adapter;
  499. struct zfcp_port *port, *tmp;
  500. u32 d_id;
  501. int ret = 0, x, last = 0;
  502. if (ct->status)
  503. return -EIO;
  504. if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
  505. if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
  506. return -EAGAIN; /* might be a temporary condition */
  507. return -EIO;
  508. }
  509. if (hdr->max_res_size) {
  510. dev_warn(&adapter->ccw_device->dev,
  511. "The name server reported %d words residual data\n",
  512. hdr->max_res_size);
  513. return -E2BIG;
  514. }
  515. mutex_lock(&zfcp_data.config_mutex);
  516. /* first entry is the header */
  517. for (x = 1; x < max_entries && !last; x++) {
  518. if (x % (ZFCP_GPN_FT_ENTRIES + 1))
  519. acc++;
  520. else
  521. acc = sg_virt(++sg);
  522. last = acc->control & 0x80;
  523. d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
  524. acc->port_id[2];
  525. /* don't attach ports with a well known address */
  526. if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
  527. continue;
  528. /* skip the adapter's port and known remote ports */
  529. if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
  530. continue;
  531. port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
  532. if (port)
  533. continue;
  534. port = zfcp_port_enqueue(adapter, acc->wwpn,
  535. ZFCP_STATUS_COMMON_NOESC, d_id);
  536. if (IS_ERR(port))
  537. ret = PTR_ERR(port);
  538. else
  539. zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
  540. }
  541. zfcp_erp_wait(adapter);
  542. list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
  543. zfcp_fc_validate_port(port);
  544. mutex_unlock(&zfcp_data.config_mutex);
  545. return ret;
  546. }
  547. /**
  548. * zfcp_fc_scan_ports - scan remote ports and attach new ports
  549. * @adapter: pointer to struct zfcp_adapter
  550. */
  551. int zfcp_fc_scan_ports(struct zfcp_adapter *adapter)
  552. {
  553. int ret, i;
  554. struct zfcp_gpn_ft *gpn_ft;
  555. int chain, max_entries, buf_num, max_bytes;
  556. chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
  557. buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
  558. max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
  559. max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
  560. if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
  561. fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
  562. return 0;
  563. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  564. if (ret)
  565. return ret;
  566. gpn_ft = zfcp_alloc_sg_env(buf_num);
  567. if (!gpn_ft) {
  568. ret = -ENOMEM;
  569. goto out;
  570. }
  571. for (i = 0; i < 3; i++) {
  572. ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
  573. if (!ret) {
  574. ret = zfcp_fc_eval_gpn_ft(gpn_ft, max_entries);
  575. if (ret == -EAGAIN)
  576. ssleep(1);
  577. else
  578. break;
  579. }
  580. }
  581. zfcp_free_sg_env(gpn_ft, buf_num);
  582. out:
  583. zfcp_fc_wka_port_put(&adapter->gs->ds);
  584. return ret;
  585. }
  586. void _zfcp_fc_scan_ports_later(struct work_struct *work)
  587. {
  588. zfcp_fc_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
  589. }
  590. struct zfcp_els_fc_job {
  591. struct zfcp_send_els els;
  592. struct fc_bsg_job *job;
  593. };
  594. static void zfcp_fc_generic_els_handler(unsigned long data)
  595. {
  596. struct zfcp_els_fc_job *els_fc_job = (struct zfcp_els_fc_job *) data;
  597. struct fc_bsg_job *job = els_fc_job->job;
  598. struct fc_bsg_reply *reply = job->reply;
  599. if (els_fc_job->els.status) {
  600. /* request rejected or timed out */
  601. reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_REJECT;
  602. goto out;
  603. }
  604. reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
  605. reply->reply_payload_rcv_len = job->reply_payload.payload_len;
  606. out:
  607. job->state_flags = FC_RQST_STATE_DONE;
  608. job->job_done(job);
  609. kfree(els_fc_job);
  610. }
  611. int zfcp_fc_execute_els_fc_job(struct fc_bsg_job *job)
  612. {
  613. struct zfcp_els_fc_job *els_fc_job;
  614. struct fc_rport *rport = job->rport;
  615. struct Scsi_Host *shost;
  616. struct zfcp_adapter *adapter;
  617. struct zfcp_port *port;
  618. u8 *port_did;
  619. shost = rport ? rport_to_shost(rport) : job->shost;
  620. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  621. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
  622. return -EINVAL;
  623. els_fc_job = kzalloc(sizeof(struct zfcp_els_fc_job), GFP_KERNEL);
  624. if (!els_fc_job)
  625. return -ENOMEM;
  626. els_fc_job->els.adapter = adapter;
  627. if (rport) {
  628. read_lock_irq(&zfcp_data.config_lock);
  629. port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
  630. if (port)
  631. els_fc_job->els.d_id = port->d_id;
  632. read_unlock_irq(&zfcp_data.config_lock);
  633. if (!port) {
  634. kfree(els_fc_job);
  635. return -EINVAL;
  636. }
  637. } else {
  638. port_did = job->request->rqst_data.h_els.port_id;
  639. els_fc_job->els.d_id = (port_did[0] << 16) +
  640. (port_did[1] << 8) + port_did[2];
  641. }
  642. els_fc_job->els.req = job->request_payload.sg_list;
  643. els_fc_job->els.resp = job->reply_payload.sg_list;
  644. els_fc_job->els.handler = zfcp_fc_generic_els_handler;
  645. els_fc_job->els.handler_data = (unsigned long) els_fc_job;
  646. els_fc_job->job = job;
  647. return zfcp_fsf_send_els(&els_fc_job->els);
  648. }
  649. struct zfcp_ct_fc_job {
  650. struct zfcp_send_ct ct;
  651. struct fc_bsg_job *job;
  652. };
  653. static void zfcp_fc_generic_ct_handler(unsigned long data)
  654. {
  655. struct zfcp_ct_fc_job *ct_fc_job = (struct zfcp_ct_fc_job *) data;
  656. struct fc_bsg_job *job = ct_fc_job->job;
  657. job->reply->reply_data.ctels_reply.status = ct_fc_job->ct.status ?
  658. FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
  659. job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
  660. job->state_flags = FC_RQST_STATE_DONE;
  661. job->job_done(job);
  662. zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
  663. kfree(ct_fc_job);
  664. }
  665. int zfcp_fc_execute_ct_fc_job(struct fc_bsg_job *job)
  666. {
  667. int ret;
  668. u8 gs_type;
  669. struct fc_rport *rport = job->rport;
  670. struct Scsi_Host *shost;
  671. struct zfcp_adapter *adapter;
  672. struct zfcp_ct_fc_job *ct_fc_job;
  673. u32 preamble_word1;
  674. shost = rport ? rport_to_shost(rport) : job->shost;
  675. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  676. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
  677. return -EINVAL;
  678. ct_fc_job = kzalloc(sizeof(struct zfcp_ct_fc_job), GFP_KERNEL);
  679. if (!ct_fc_job)
  680. return -ENOMEM;
  681. preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
  682. gs_type = (preamble_word1 & 0xff000000) >> 24;
  683. switch (gs_type) {
  684. case FC_FST_ALIAS:
  685. ct_fc_job->ct.wka_port = &adapter->gs->as;
  686. break;
  687. case FC_FST_MGMT:
  688. ct_fc_job->ct.wka_port = &adapter->gs->ms;
  689. break;
  690. case FC_FST_TIME:
  691. ct_fc_job->ct.wka_port = &adapter->gs->ts;
  692. break;
  693. case FC_FST_DIR:
  694. ct_fc_job->ct.wka_port = &adapter->gs->ds;
  695. break;
  696. default:
  697. kfree(ct_fc_job);
  698. return -EINVAL; /* no such service */
  699. }
  700. ret = zfcp_fc_wka_port_get(ct_fc_job->ct.wka_port);
  701. if (ret) {
  702. kfree(ct_fc_job);
  703. return ret;
  704. }
  705. ct_fc_job->ct.req = job->request_payload.sg_list;
  706. ct_fc_job->ct.resp = job->reply_payload.sg_list;
  707. ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
  708. ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
  709. ct_fc_job->ct.completion = NULL;
  710. ct_fc_job->job = job;
  711. ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL);
  712. if (ret) {
  713. kfree(ct_fc_job);
  714. zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
  715. }
  716. return ret;
  717. }
  718. int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
  719. {
  720. struct zfcp_wka_ports *wka_ports;
  721. wka_ports = kzalloc(sizeof(struct zfcp_wka_ports), GFP_KERNEL);
  722. if (!wka_ports)
  723. return -ENOMEM;
  724. adapter->gs = wka_ports;
  725. zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
  726. zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
  727. zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
  728. zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
  729. zfcp_fc_wka_port_init(&wka_ports->ks, FC_FID_SEC_KEY, adapter);
  730. return 0;
  731. }
  732. void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
  733. {
  734. kfree(adapter->gs);
  735. adapter->gs = NULL;
  736. }