zfcp_fc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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.timeout = ZFCP_NS_GID_PN_TIMEOUT;
  240. gid_pn->ct.req = &gid_pn->req;
  241. gid_pn->ct.resp = &gid_pn->resp;
  242. sg_init_one(&gid_pn->req, &gid_pn->ct_iu_req,
  243. sizeof(struct ct_iu_gid_pn_req));
  244. sg_init_one(&gid_pn->resp, &gid_pn->ct_iu_resp,
  245. sizeof(struct ct_iu_gid_pn_resp));
  246. /* setup nameserver request */
  247. gid_pn->ct_iu_req.header.revision = ZFCP_CT_REVISION;
  248. gid_pn->ct_iu_req.header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  249. gid_pn->ct_iu_req.header.gs_subtype = ZFCP_CT_NAME_SERVER;
  250. gid_pn->ct_iu_req.header.options = ZFCP_CT_SYNCHRONOUS;
  251. gid_pn->ct_iu_req.header.cmd_rsp_code = ZFCP_CT_GID_PN;
  252. gid_pn->ct_iu_req.header.max_res_size = ZFCP_CT_SIZE_ONE_PAGE / 4;
  253. gid_pn->ct_iu_req.wwpn = port->wwpn;
  254. init_completion(&compl_rec.done);
  255. compl_rec.handler = zfcp_fc_ns_gid_pn_eval;
  256. compl_rec.handler_data = (unsigned long) gid_pn;
  257. ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.gid_pn_req);
  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. * @port: port where GID_PN request is needed
  265. * return: -ENOMEM on error, 0 otherwise
  266. */
  267. static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
  268. {
  269. int ret;
  270. struct zfcp_gid_pn_data *gid_pn;
  271. struct zfcp_adapter *adapter = port->adapter;
  272. gid_pn = mempool_alloc(adapter->pool.gid_pn_data, GFP_ATOMIC);
  273. if (!gid_pn)
  274. return -ENOMEM;
  275. memset(gid_pn, 0, sizeof(*gid_pn));
  276. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  277. if (ret)
  278. goto out;
  279. ret = zfcp_fc_ns_gid_pn_request(port, gid_pn);
  280. zfcp_fc_wka_port_put(&adapter->gs->ds);
  281. out:
  282. mempool_free(gid_pn, adapter->pool.gid_pn_data);
  283. return ret;
  284. }
  285. void zfcp_fc_port_did_lookup(struct work_struct *work)
  286. {
  287. int ret;
  288. struct zfcp_port *port = container_of(work, struct zfcp_port,
  289. gid_pn_work);
  290. ret = zfcp_fc_ns_gid_pn(port);
  291. if (ret) {
  292. /* could not issue gid_pn for some reason */
  293. zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1", NULL);
  294. goto out;
  295. }
  296. if (!port->d_id) {
  297. zfcp_erp_port_failed(port, "fcgpn_2", NULL);
  298. goto out;
  299. }
  300. zfcp_erp_port_reopen(port, 0, "fcgpn_3", NULL);
  301. out:
  302. zfcp_port_put(port);
  303. }
  304. /**
  305. * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
  306. * @port: zfcp_port structure
  307. * @plogi: plogi payload
  308. *
  309. * Evaluate PLOGI playload and copy important fields into zfcp_port structure
  310. */
  311. void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
  312. {
  313. port->maxframe_size = plogi->serv_param.common_serv_param[7] |
  314. ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
  315. if (plogi->serv_param.class1_serv_param[0] & 0x80)
  316. port->supported_classes |= FC_COS_CLASS1;
  317. if (plogi->serv_param.class2_serv_param[0] & 0x80)
  318. port->supported_classes |= FC_COS_CLASS2;
  319. if (plogi->serv_param.class3_serv_param[0] & 0x80)
  320. port->supported_classes |= FC_COS_CLASS3;
  321. if (plogi->serv_param.class4_serv_param[0] & 0x80)
  322. port->supported_classes |= FC_COS_CLASS4;
  323. }
  324. struct zfcp_els_adisc {
  325. struct zfcp_send_els els;
  326. struct scatterlist req;
  327. struct scatterlist resp;
  328. struct zfcp_ls_adisc ls_adisc;
  329. struct zfcp_ls_adisc ls_adisc_acc;
  330. };
  331. static void zfcp_fc_adisc_handler(unsigned long data)
  332. {
  333. struct zfcp_els_adisc *adisc = (struct zfcp_els_adisc *) data;
  334. struct zfcp_port *port = adisc->els.port;
  335. struct zfcp_ls_adisc *ls_adisc = &adisc->ls_adisc_acc;
  336. if (adisc->els.status) {
  337. /* request rejected or timed out */
  338. zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  339. "fcadh_1", NULL);
  340. goto out;
  341. }
  342. if (!port->wwnn)
  343. port->wwnn = ls_adisc->wwnn;
  344. if ((port->wwpn != ls_adisc->wwpn) ||
  345. !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
  346. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  347. "fcadh_2", NULL);
  348. goto out;
  349. }
  350. /* port is good, unblock rport without going through erp */
  351. zfcp_scsi_schedule_rport_register(port);
  352. out:
  353. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  354. zfcp_port_put(port);
  355. kfree(adisc);
  356. }
  357. static int zfcp_fc_adisc(struct zfcp_port *port)
  358. {
  359. struct zfcp_els_adisc *adisc;
  360. struct zfcp_adapter *adapter = port->adapter;
  361. adisc = kzalloc(sizeof(struct zfcp_els_adisc), GFP_ATOMIC);
  362. if (!adisc)
  363. return -ENOMEM;
  364. adisc->els.req = &adisc->req;
  365. adisc->els.resp = &adisc->resp;
  366. sg_init_one(adisc->els.req, &adisc->ls_adisc,
  367. sizeof(struct zfcp_ls_adisc));
  368. sg_init_one(adisc->els.resp, &adisc->ls_adisc_acc,
  369. sizeof(struct zfcp_ls_adisc));
  370. adisc->els.adapter = adapter;
  371. adisc->els.port = port;
  372. adisc->els.d_id = port->d_id;
  373. adisc->els.handler = zfcp_fc_adisc_handler;
  374. adisc->els.handler_data = (unsigned long) adisc;
  375. adisc->els.ls_code = adisc->ls_adisc.code = ZFCP_LS_ADISC;
  376. /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
  377. without FC-AL-2 capability, so we don't set it */
  378. adisc->ls_adisc.wwpn = fc_host_port_name(adapter->scsi_host);
  379. adisc->ls_adisc.wwnn = fc_host_node_name(adapter->scsi_host);
  380. adisc->ls_adisc.nport_id = fc_host_port_id(adapter->scsi_host);
  381. return zfcp_fsf_send_els(&adisc->els);
  382. }
  383. void zfcp_fc_link_test_work(struct work_struct *work)
  384. {
  385. struct zfcp_port *port =
  386. container_of(work, struct zfcp_port, test_link_work);
  387. int retval;
  388. zfcp_port_get(port);
  389. port->rport_task = RPORT_DEL;
  390. zfcp_scsi_rport_work(&port->rport_work);
  391. /* only issue one test command at one time per port */
  392. if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
  393. goto out;
  394. atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  395. retval = zfcp_fc_adisc(port);
  396. if (retval == 0)
  397. return;
  398. /* send of ADISC was not possible */
  399. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  400. zfcp_erp_port_forced_reopen(port, 0, "fcltwk1", NULL);
  401. out:
  402. zfcp_port_put(port);
  403. }
  404. /**
  405. * zfcp_fc_test_link - lightweight link test procedure
  406. * @port: port to be tested
  407. *
  408. * Test status of a link to a remote port using the ELS command ADISC.
  409. * If there is a problem with the remote port, error recovery steps
  410. * will be triggered.
  411. */
  412. void zfcp_fc_test_link(struct zfcp_port *port)
  413. {
  414. zfcp_port_get(port);
  415. if (!queue_work(port->adapter->work_queue, &port->test_link_work))
  416. zfcp_port_put(port);
  417. }
  418. static void zfcp_free_sg_env(struct zfcp_gpn_ft *gpn_ft, int buf_num)
  419. {
  420. struct scatterlist *sg = &gpn_ft->sg_req;
  421. kmem_cache_free(zfcp_data.gpn_ft_cache, sg_virt(sg));
  422. zfcp_sg_free_table(gpn_ft->sg_resp, buf_num);
  423. kfree(gpn_ft);
  424. }
  425. static struct zfcp_gpn_ft *zfcp_alloc_sg_env(int buf_num)
  426. {
  427. struct zfcp_gpn_ft *gpn_ft;
  428. struct ct_iu_gpn_ft_req *req;
  429. gpn_ft = kzalloc(sizeof(*gpn_ft), GFP_KERNEL);
  430. if (!gpn_ft)
  431. return NULL;
  432. req = kmem_cache_alloc(zfcp_data.gpn_ft_cache, GFP_KERNEL);
  433. if (!req) {
  434. kfree(gpn_ft);
  435. gpn_ft = NULL;
  436. goto out;
  437. }
  438. sg_init_one(&gpn_ft->sg_req, req, sizeof(*req));
  439. if (zfcp_sg_setup_table(gpn_ft->sg_resp, buf_num)) {
  440. zfcp_free_sg_env(gpn_ft, buf_num);
  441. gpn_ft = NULL;
  442. }
  443. out:
  444. return gpn_ft;
  445. }
  446. static int zfcp_fc_send_gpn_ft(struct zfcp_gpn_ft *gpn_ft,
  447. struct zfcp_adapter *adapter, int max_bytes)
  448. {
  449. struct zfcp_send_ct *ct = &gpn_ft->ct;
  450. struct ct_iu_gpn_ft_req *req = sg_virt(&gpn_ft->sg_req);
  451. struct zfcp_fc_ns_handler_data compl_rec;
  452. int ret;
  453. /* prepare CT IU for GPN_FT */
  454. req->header.revision = ZFCP_CT_REVISION;
  455. req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  456. req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
  457. req->header.options = ZFCP_CT_SYNCHRONOUS;
  458. req->header.cmd_rsp_code = ZFCP_CT_GPN_FT;
  459. req->header.max_res_size = max_bytes / 4;
  460. req->flags = 0;
  461. req->domain_id_scope = 0;
  462. req->area_id_scope = 0;
  463. req->fc4_type = ZFCP_CT_SCSI_FCP;
  464. /* prepare zfcp_send_ct */
  465. ct->wka_port = &adapter->gs->ds;
  466. ct->handler = zfcp_fc_ns_handler;
  467. ct->handler_data = (unsigned long)&compl_rec;
  468. ct->timeout = 10;
  469. ct->req = &gpn_ft->sg_req;
  470. ct->resp = gpn_ft->sg_resp;
  471. init_completion(&compl_rec.done);
  472. compl_rec.handler = NULL;
  473. ret = zfcp_fsf_send_ct(ct, NULL);
  474. if (!ret)
  475. wait_for_completion(&compl_rec.done);
  476. return ret;
  477. }
  478. static void zfcp_fc_validate_port(struct zfcp_port *port)
  479. {
  480. struct zfcp_adapter *adapter = port->adapter;
  481. if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
  482. return;
  483. atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
  484. if ((port->supported_classes != 0) ||
  485. !list_empty(&port->unit_list_head)) {
  486. zfcp_port_put(port);
  487. return;
  488. }
  489. zfcp_erp_port_shutdown(port, 0, "fcpval1", NULL);
  490. zfcp_erp_wait(adapter);
  491. zfcp_port_put(port);
  492. zfcp_port_dequeue(port);
  493. }
  494. static int zfcp_fc_eval_gpn_ft(struct zfcp_gpn_ft *gpn_ft, int max_entries)
  495. {
  496. struct zfcp_send_ct *ct = &gpn_ft->ct;
  497. struct scatterlist *sg = gpn_ft->sg_resp;
  498. struct ct_hdr *hdr = sg_virt(sg);
  499. struct gpn_ft_resp_acc *acc = sg_virt(sg);
  500. struct zfcp_adapter *adapter = ct->wka_port->adapter;
  501. struct zfcp_port *port, *tmp;
  502. u32 d_id;
  503. int ret = 0, x, last = 0;
  504. if (ct->status)
  505. return -EIO;
  506. if (hdr->cmd_rsp_code != ZFCP_CT_ACCEPT) {
  507. if (hdr->reason_code == ZFCP_CT_UNABLE_TO_PERFORM_CMD)
  508. return -EAGAIN; /* might be a temporary condition */
  509. return -EIO;
  510. }
  511. if (hdr->max_res_size) {
  512. dev_warn(&adapter->ccw_device->dev,
  513. "The name server reported %d words residual data\n",
  514. hdr->max_res_size);
  515. return -E2BIG;
  516. }
  517. down(&zfcp_data.config_sema);
  518. /* first entry is the header */
  519. for (x = 1; x < max_entries && !last; x++) {
  520. if (x % (ZFCP_GPN_FT_ENTRIES + 1))
  521. acc++;
  522. else
  523. acc = sg_virt(++sg);
  524. last = acc->control & 0x80;
  525. d_id = acc->port_id[0] << 16 | acc->port_id[1] << 8 |
  526. acc->port_id[2];
  527. /* don't attach ports with a well known address */
  528. if ((d_id & ZFCP_DID_WKA) == ZFCP_DID_WKA)
  529. continue;
  530. /* skip the adapter's port and known remote ports */
  531. if (acc->wwpn == fc_host_port_name(adapter->scsi_host))
  532. continue;
  533. port = zfcp_get_port_by_wwpn(adapter, acc->wwpn);
  534. if (port)
  535. continue;
  536. port = zfcp_port_enqueue(adapter, acc->wwpn,
  537. ZFCP_STATUS_COMMON_NOESC, d_id);
  538. if (IS_ERR(port))
  539. ret = PTR_ERR(port);
  540. else
  541. zfcp_erp_port_reopen(port, 0, "fcegpf1", NULL);
  542. }
  543. zfcp_erp_wait(adapter);
  544. list_for_each_entry_safe(port, tmp, &adapter->port_list_head, list)
  545. zfcp_fc_validate_port(port);
  546. up(&zfcp_data.config_sema);
  547. return ret;
  548. }
  549. /**
  550. * zfcp_fc_scan_ports - scan remote ports and attach new ports
  551. * @adapter: pointer to struct zfcp_adapter
  552. */
  553. int zfcp_fc_scan_ports(struct zfcp_adapter *adapter)
  554. {
  555. int ret, i;
  556. struct zfcp_gpn_ft *gpn_ft;
  557. int chain, max_entries, buf_num, max_bytes;
  558. chain = adapter->adapter_features & FSF_FEATURE_ELS_CT_CHAINED_SBALS;
  559. buf_num = chain ? ZFCP_GPN_FT_BUFFERS : 1;
  560. max_entries = chain ? ZFCP_GPN_FT_MAX_ENTRIES : ZFCP_GPN_FT_ENTRIES;
  561. max_bytes = chain ? ZFCP_GPN_FT_MAX_SIZE : ZFCP_CT_SIZE_ONE_PAGE;
  562. if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
  563. fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
  564. return 0;
  565. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  566. if (ret)
  567. return ret;
  568. gpn_ft = zfcp_alloc_sg_env(buf_num);
  569. if (!gpn_ft) {
  570. ret = -ENOMEM;
  571. goto out;
  572. }
  573. for (i = 0; i < 3; i++) {
  574. ret = zfcp_fc_send_gpn_ft(gpn_ft, adapter, max_bytes);
  575. if (!ret) {
  576. ret = zfcp_fc_eval_gpn_ft(gpn_ft, max_entries);
  577. if (ret == -EAGAIN)
  578. ssleep(1);
  579. else
  580. break;
  581. }
  582. }
  583. zfcp_free_sg_env(gpn_ft, buf_num);
  584. out:
  585. zfcp_fc_wka_port_put(&adapter->gs->ds);
  586. return ret;
  587. }
  588. void _zfcp_fc_scan_ports_later(struct work_struct *work)
  589. {
  590. zfcp_fc_scan_ports(container_of(work, struct zfcp_adapter, scan_work));
  591. }
  592. struct zfcp_els_fc_job {
  593. struct zfcp_send_els els;
  594. struct fc_bsg_job *job;
  595. };
  596. static void zfcp_fc_generic_els_handler(unsigned long data)
  597. {
  598. struct zfcp_els_fc_job *els_fc_job = (struct zfcp_els_fc_job *) data;
  599. struct fc_bsg_job *job = els_fc_job->job;
  600. struct fc_bsg_reply *reply = job->reply;
  601. if (els_fc_job->els.status) {
  602. /* request rejected or timed out */
  603. reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_REJECT;
  604. goto out;
  605. }
  606. reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
  607. reply->reply_payload_rcv_len = job->reply_payload.payload_len;
  608. out:
  609. job->state_flags = FC_RQST_STATE_DONE;
  610. job->job_done(job);
  611. kfree(els_fc_job);
  612. }
  613. int zfcp_fc_execute_els_fc_job(struct fc_bsg_job *job)
  614. {
  615. struct zfcp_els_fc_job *els_fc_job;
  616. struct fc_rport *rport = job->rport;
  617. struct Scsi_Host *shost;
  618. struct zfcp_adapter *adapter;
  619. struct zfcp_port *port;
  620. u8 *port_did;
  621. shost = rport ? rport_to_shost(rport) : job->shost;
  622. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  623. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
  624. return -EINVAL;
  625. els_fc_job = kzalloc(sizeof(struct zfcp_els_fc_job), GFP_KERNEL);
  626. if (!els_fc_job)
  627. return -ENOMEM;
  628. els_fc_job->els.adapter = adapter;
  629. if (rport) {
  630. read_lock_irq(&zfcp_data.config_lock);
  631. port = rport->dd_data;
  632. if (port)
  633. els_fc_job->els.d_id = port->d_id;
  634. read_unlock_irq(&zfcp_data.config_lock);
  635. if (!port) {
  636. kfree(els_fc_job);
  637. return -EINVAL;
  638. }
  639. } else {
  640. port_did = job->request->rqst_data.h_els.port_id;
  641. els_fc_job->els.d_id = (port_did[0] << 16) +
  642. (port_did[1] << 8) + port_did[2];
  643. }
  644. els_fc_job->els.req = job->request_payload.sg_list;
  645. els_fc_job->els.resp = job->reply_payload.sg_list;
  646. els_fc_job->els.handler = zfcp_fc_generic_els_handler;
  647. els_fc_job->els.handler_data = (unsigned long) els_fc_job;
  648. els_fc_job->job = job;
  649. return zfcp_fsf_send_els(&els_fc_job->els);
  650. }
  651. struct zfcp_ct_fc_job {
  652. struct zfcp_send_ct ct;
  653. struct fc_bsg_job *job;
  654. };
  655. static void zfcp_fc_generic_ct_handler(unsigned long data)
  656. {
  657. struct zfcp_ct_fc_job *ct_fc_job = (struct zfcp_ct_fc_job *) data;
  658. struct fc_bsg_job *job = ct_fc_job->job;
  659. job->reply->reply_data.ctels_reply.status = ct_fc_job->ct.status ?
  660. FC_CTELS_STATUS_REJECT : FC_CTELS_STATUS_OK;
  661. job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
  662. job->state_flags = FC_RQST_STATE_DONE;
  663. job->job_done(job);
  664. zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
  665. kfree(ct_fc_job);
  666. }
  667. int zfcp_fc_execute_ct_fc_job(struct fc_bsg_job *job)
  668. {
  669. int ret;
  670. u8 gs_type;
  671. struct fc_rport *rport = job->rport;
  672. struct Scsi_Host *shost;
  673. struct zfcp_adapter *adapter;
  674. struct zfcp_ct_fc_job *ct_fc_job;
  675. u32 preamble_word1;
  676. shost = rport ? rport_to_shost(rport) : job->shost;
  677. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  678. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
  679. return -EINVAL;
  680. ct_fc_job = kzalloc(sizeof(struct zfcp_ct_fc_job), GFP_KERNEL);
  681. if (!ct_fc_job)
  682. return -ENOMEM;
  683. preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
  684. gs_type = (preamble_word1 & 0xff000000) >> 24;
  685. switch (gs_type) {
  686. case FC_FST_ALIAS:
  687. ct_fc_job->ct.wka_port = &adapter->gs->as;
  688. break;
  689. case FC_FST_MGMT:
  690. ct_fc_job->ct.wka_port = &adapter->gs->ms;
  691. break;
  692. case FC_FST_TIME:
  693. ct_fc_job->ct.wka_port = &adapter->gs->ts;
  694. break;
  695. case FC_FST_DIR:
  696. ct_fc_job->ct.wka_port = &adapter->gs->ds;
  697. break;
  698. default:
  699. kfree(ct_fc_job);
  700. return -EINVAL; /* no such service */
  701. }
  702. ret = zfcp_fc_wka_port_get(ct_fc_job->ct.wka_port);
  703. if (ret) {
  704. kfree(ct_fc_job);
  705. return ret;
  706. }
  707. ct_fc_job->ct.req = job->request_payload.sg_list;
  708. ct_fc_job->ct.resp = job->reply_payload.sg_list;
  709. ct_fc_job->ct.timeout = ZFCP_FSF_REQUEST_TIMEOUT;
  710. ct_fc_job->ct.handler = zfcp_fc_generic_ct_handler;
  711. ct_fc_job->ct.handler_data = (unsigned long) ct_fc_job;
  712. ct_fc_job->ct.completion = NULL;
  713. ct_fc_job->job = job;
  714. ret = zfcp_fsf_send_ct(&ct_fc_job->ct, NULL);
  715. if (ret) {
  716. kfree(ct_fc_job);
  717. zfcp_fc_wka_port_put(ct_fc_job->ct.wka_port);
  718. }
  719. return ret;
  720. }
  721. int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
  722. {
  723. struct zfcp_wka_ports *wka_ports;
  724. wka_ports = kzalloc(sizeof(struct zfcp_wka_ports), GFP_KERNEL);
  725. if (!wka_ports)
  726. return -ENOMEM;
  727. adapter->gs = wka_ports;
  728. zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
  729. zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
  730. zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
  731. zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
  732. zfcp_fc_wka_port_init(&wka_ports->ks, FC_FID_SEC_KEY, adapter);
  733. return 0;
  734. }
  735. void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
  736. {
  737. kfree(adapter->gs);
  738. adapter->gs = NULL;
  739. }