zfcp_fc.c 24 KB

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