zfcp_fc.c 23 KB

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