zfcp_fc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * zfcp device driver
  3. *
  4. * Fibre Channel related functions for the zfcp device driver.
  5. *
  6. * Copyright IBM Corporation 2008, 2010
  7. */
  8. #define KMSG_COMPONENT "zfcp"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/types.h>
  11. #include <linux/slab.h>
  12. #include <linux/utsname.h>
  13. #include <scsi/fc/fc_els.h>
  14. #include <scsi/libfc.h>
  15. #include "zfcp_ext.h"
  16. #include "zfcp_fc.h"
  17. struct kmem_cache *zfcp_fc_req_cache;
  18. static u32 zfcp_fc_rscn_range_mask[] = {
  19. [ELS_ADDR_FMT_PORT] = 0xFFFFFF,
  20. [ELS_ADDR_FMT_AREA] = 0xFFFF00,
  21. [ELS_ADDR_FMT_DOM] = 0xFF0000,
  22. [ELS_ADDR_FMT_FAB] = 0x000000,
  23. };
  24. /**
  25. * zfcp_fc_post_event - post event to userspace via fc_transport
  26. * @work: work struct with enqueued events
  27. */
  28. void zfcp_fc_post_event(struct work_struct *work)
  29. {
  30. struct zfcp_fc_event *event = NULL, *tmp = NULL;
  31. LIST_HEAD(tmp_lh);
  32. struct zfcp_fc_events *events = container_of(work,
  33. struct zfcp_fc_events, work);
  34. struct zfcp_adapter *adapter = container_of(events, struct zfcp_adapter,
  35. events);
  36. spin_lock_bh(&events->list_lock);
  37. list_splice_init(&events->list, &tmp_lh);
  38. spin_unlock_bh(&events->list_lock);
  39. list_for_each_entry_safe(event, tmp, &tmp_lh, list) {
  40. fc_host_post_event(adapter->scsi_host, fc_get_event_number(),
  41. event->code, event->data);
  42. list_del(&event->list);
  43. kfree(event);
  44. }
  45. }
  46. /**
  47. * zfcp_fc_enqueue_event - safely enqueue FC HBA API event from irq context
  48. * @adapter: The adapter where to enqueue the event
  49. * @event_code: The event code (as defined in fc_host_event_code in
  50. * scsi_transport_fc.h)
  51. * @event_data: The event data (e.g. n_port page in case of els)
  52. */
  53. void zfcp_fc_enqueue_event(struct zfcp_adapter *adapter,
  54. enum fc_host_event_code event_code, u32 event_data)
  55. {
  56. struct zfcp_fc_event *event;
  57. event = kmalloc(sizeof(struct zfcp_fc_event), GFP_ATOMIC);
  58. if (!event)
  59. return;
  60. event->code = event_code;
  61. event->data = event_data;
  62. spin_lock(&adapter->events.list_lock);
  63. list_add_tail(&event->list, &adapter->events.list);
  64. spin_unlock(&adapter->events.list_lock);
  65. queue_work(adapter->work_queue, &adapter->events.work);
  66. }
  67. static int zfcp_fc_wka_port_get(struct zfcp_fc_wka_port *wka_port)
  68. {
  69. if (mutex_lock_interruptible(&wka_port->mutex))
  70. return -ERESTARTSYS;
  71. if (wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE ||
  72. wka_port->status == ZFCP_FC_WKA_PORT_CLOSING) {
  73. wka_port->status = ZFCP_FC_WKA_PORT_OPENING;
  74. if (zfcp_fsf_open_wka_port(wka_port))
  75. wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
  76. }
  77. mutex_unlock(&wka_port->mutex);
  78. wait_event(wka_port->completion_wq,
  79. wka_port->status == ZFCP_FC_WKA_PORT_ONLINE ||
  80. wka_port->status == ZFCP_FC_WKA_PORT_OFFLINE);
  81. if (wka_port->status == ZFCP_FC_WKA_PORT_ONLINE) {
  82. atomic_inc(&wka_port->refcount);
  83. return 0;
  84. }
  85. return -EIO;
  86. }
  87. static void zfcp_fc_wka_port_offline(struct work_struct *work)
  88. {
  89. struct delayed_work *dw = to_delayed_work(work);
  90. struct zfcp_fc_wka_port *wka_port =
  91. container_of(dw, struct zfcp_fc_wka_port, work);
  92. mutex_lock(&wka_port->mutex);
  93. if ((atomic_read(&wka_port->refcount) != 0) ||
  94. (wka_port->status != ZFCP_FC_WKA_PORT_ONLINE))
  95. goto out;
  96. wka_port->status = ZFCP_FC_WKA_PORT_CLOSING;
  97. if (zfcp_fsf_close_wka_port(wka_port)) {
  98. wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
  99. wake_up(&wka_port->completion_wq);
  100. }
  101. out:
  102. mutex_unlock(&wka_port->mutex);
  103. }
  104. static void zfcp_fc_wka_port_put(struct zfcp_fc_wka_port *wka_port)
  105. {
  106. if (atomic_dec_return(&wka_port->refcount) != 0)
  107. return;
  108. /* wait 10 milliseconds, other reqs might pop in */
  109. schedule_delayed_work(&wka_port->work, HZ / 100);
  110. }
  111. static void zfcp_fc_wka_port_init(struct zfcp_fc_wka_port *wka_port, u32 d_id,
  112. struct zfcp_adapter *adapter)
  113. {
  114. init_waitqueue_head(&wka_port->completion_wq);
  115. wka_port->adapter = adapter;
  116. wka_port->d_id = d_id;
  117. wka_port->status = ZFCP_FC_WKA_PORT_OFFLINE;
  118. atomic_set(&wka_port->refcount, 0);
  119. mutex_init(&wka_port->mutex);
  120. INIT_DELAYED_WORK(&wka_port->work, zfcp_fc_wka_port_offline);
  121. }
  122. static void zfcp_fc_wka_port_force_offline(struct zfcp_fc_wka_port *wka)
  123. {
  124. cancel_delayed_work_sync(&wka->work);
  125. mutex_lock(&wka->mutex);
  126. wka->status = ZFCP_FC_WKA_PORT_OFFLINE;
  127. mutex_unlock(&wka->mutex);
  128. }
  129. void zfcp_fc_wka_ports_force_offline(struct zfcp_fc_wka_ports *gs)
  130. {
  131. if (!gs)
  132. return;
  133. zfcp_fc_wka_port_force_offline(&gs->ms);
  134. zfcp_fc_wka_port_force_offline(&gs->ts);
  135. zfcp_fc_wka_port_force_offline(&gs->ds);
  136. zfcp_fc_wka_port_force_offline(&gs->as);
  137. }
  138. static void _zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req, u32 range,
  139. struct fc_els_rscn_page *page)
  140. {
  141. unsigned long flags;
  142. struct zfcp_adapter *adapter = fsf_req->adapter;
  143. struct zfcp_port *port;
  144. read_lock_irqsave(&adapter->port_list_lock, flags);
  145. list_for_each_entry(port, &adapter->port_list, list) {
  146. if ((port->d_id & range) == (ntoh24(page->rscn_fid) & range))
  147. zfcp_fc_test_link(port);
  148. if (!port->d_id)
  149. zfcp_erp_port_reopen(port,
  150. ZFCP_STATUS_COMMON_ERP_FAILED,
  151. "fcrscn1");
  152. }
  153. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  154. }
  155. static void zfcp_fc_incoming_rscn(struct zfcp_fsf_req *fsf_req)
  156. {
  157. struct fsf_status_read_buffer *status_buffer = (void *)fsf_req->data;
  158. struct fc_els_rscn *head;
  159. struct fc_els_rscn_page *page;
  160. u16 i;
  161. u16 no_entries;
  162. unsigned int afmt;
  163. head = (struct fc_els_rscn *) status_buffer->payload.data;
  164. page = (struct fc_els_rscn_page *) head;
  165. /* see FC-FS */
  166. no_entries = head->rscn_plen / sizeof(struct fc_els_rscn_page);
  167. for (i = 1; i < no_entries; i++) {
  168. /* skip head and start with 1st element */
  169. page++;
  170. afmt = page->rscn_page_flags & ELS_RSCN_ADDR_FMT_MASK;
  171. _zfcp_fc_incoming_rscn(fsf_req, zfcp_fc_rscn_range_mask[afmt],
  172. page);
  173. zfcp_fc_enqueue_event(fsf_req->adapter, FCH_EVT_RSCN,
  174. *(u32 *)page);
  175. }
  176. queue_work(fsf_req->adapter->work_queue, &fsf_req->adapter->scan_work);
  177. }
  178. static void zfcp_fc_incoming_wwpn(struct zfcp_fsf_req *req, u64 wwpn)
  179. {
  180. unsigned long flags;
  181. struct zfcp_adapter *adapter = req->adapter;
  182. struct zfcp_port *port;
  183. read_lock_irqsave(&adapter->port_list_lock, flags);
  184. list_for_each_entry(port, &adapter->port_list, list)
  185. if (port->wwpn == wwpn) {
  186. zfcp_erp_port_forced_reopen(port, 0, "fciwwp1");
  187. break;
  188. }
  189. read_unlock_irqrestore(&adapter->port_list_lock, flags);
  190. }
  191. static void zfcp_fc_incoming_plogi(struct zfcp_fsf_req *req)
  192. {
  193. struct fsf_status_read_buffer *status_buffer;
  194. struct fc_els_flogi *plogi;
  195. status_buffer = (struct fsf_status_read_buffer *) req->data;
  196. plogi = (struct fc_els_flogi *) status_buffer->payload.data;
  197. zfcp_fc_incoming_wwpn(req, plogi->fl_wwpn);
  198. }
  199. static void zfcp_fc_incoming_logo(struct zfcp_fsf_req *req)
  200. {
  201. struct fsf_status_read_buffer *status_buffer =
  202. (struct fsf_status_read_buffer *)req->data;
  203. struct fc_els_logo *logo =
  204. (struct fc_els_logo *) status_buffer->payload.data;
  205. zfcp_fc_incoming_wwpn(req, logo->fl_n_port_wwn);
  206. }
  207. /**
  208. * zfcp_fc_incoming_els - handle incoming ELS
  209. * @fsf_req - request which contains incoming ELS
  210. */
  211. void zfcp_fc_incoming_els(struct zfcp_fsf_req *fsf_req)
  212. {
  213. struct fsf_status_read_buffer *status_buffer =
  214. (struct fsf_status_read_buffer *) fsf_req->data;
  215. unsigned int els_type = status_buffer->payload.data[0];
  216. zfcp_dbf_san_in_els("fciels1", fsf_req);
  217. if (els_type == ELS_PLOGI)
  218. zfcp_fc_incoming_plogi(fsf_req);
  219. else if (els_type == ELS_LOGO)
  220. zfcp_fc_incoming_logo(fsf_req);
  221. else if (els_type == ELS_RSCN)
  222. zfcp_fc_incoming_rscn(fsf_req);
  223. }
  224. static void zfcp_fc_ns_gid_pn_eval(struct zfcp_fc_req *fc_req)
  225. {
  226. struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
  227. struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
  228. if (ct_els->status)
  229. return;
  230. if (gid_pn_rsp->ct_hdr.ct_cmd != FC_FS_ACC)
  231. return;
  232. /* looks like a valid d_id */
  233. ct_els->port->d_id = ntoh24(gid_pn_rsp->gid_pn.fp_fid);
  234. }
  235. static void zfcp_fc_complete(void *data)
  236. {
  237. complete(data);
  238. }
  239. static void zfcp_fc_ct_ns_init(struct fc_ct_hdr *ct_hdr, u16 cmd, u16 mr_size)
  240. {
  241. ct_hdr->ct_rev = FC_CT_REV;
  242. ct_hdr->ct_fs_type = FC_FST_DIR;
  243. ct_hdr->ct_fs_subtype = FC_NS_SUBTYPE;
  244. ct_hdr->ct_cmd = cmd;
  245. ct_hdr->ct_mr_size = mr_size / 4;
  246. }
  247. static int zfcp_fc_ns_gid_pn_request(struct zfcp_port *port,
  248. struct zfcp_fc_req *fc_req)
  249. {
  250. struct zfcp_adapter *adapter = port->adapter;
  251. DECLARE_COMPLETION_ONSTACK(completion);
  252. struct zfcp_fc_gid_pn_req *gid_pn_req = &fc_req->u.gid_pn.req;
  253. struct zfcp_fc_gid_pn_rsp *gid_pn_rsp = &fc_req->u.gid_pn.rsp;
  254. int ret;
  255. /* setup parameters for send generic command */
  256. fc_req->ct_els.port = port;
  257. fc_req->ct_els.handler = zfcp_fc_complete;
  258. fc_req->ct_els.handler_data = &completion;
  259. fc_req->ct_els.req = &fc_req->sg_req;
  260. fc_req->ct_els.resp = &fc_req->sg_rsp;
  261. sg_init_one(&fc_req->sg_req, gid_pn_req, sizeof(*gid_pn_req));
  262. sg_init_one(&fc_req->sg_rsp, gid_pn_rsp, sizeof(*gid_pn_rsp));
  263. zfcp_fc_ct_ns_init(&gid_pn_req->ct_hdr,
  264. FC_NS_GID_PN, ZFCP_FC_CT_SIZE_PAGE);
  265. gid_pn_req->gid_pn.fn_wwpn = port->wwpn;
  266. ret = zfcp_fsf_send_ct(&adapter->gs->ds, &fc_req->ct_els,
  267. adapter->pool.gid_pn_req,
  268. ZFCP_FC_CTELS_TMO);
  269. if (!ret) {
  270. wait_for_completion(&completion);
  271. zfcp_fc_ns_gid_pn_eval(fc_req);
  272. }
  273. return ret;
  274. }
  275. /**
  276. * zfcp_fc_ns_gid_pn - initiate GID_PN nameserver request
  277. * @port: port where GID_PN request is needed
  278. * return: -ENOMEM on error, 0 otherwise
  279. */
  280. static int zfcp_fc_ns_gid_pn(struct zfcp_port *port)
  281. {
  282. int ret;
  283. struct zfcp_fc_req *fc_req;
  284. struct zfcp_adapter *adapter = port->adapter;
  285. fc_req = mempool_alloc(adapter->pool.gid_pn, GFP_ATOMIC);
  286. if (!fc_req)
  287. return -ENOMEM;
  288. memset(fc_req, 0, sizeof(*fc_req));
  289. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  290. if (ret)
  291. goto out;
  292. ret = zfcp_fc_ns_gid_pn_request(port, fc_req);
  293. zfcp_fc_wka_port_put(&adapter->gs->ds);
  294. out:
  295. mempool_free(fc_req, adapter->pool.gid_pn);
  296. return ret;
  297. }
  298. void zfcp_fc_port_did_lookup(struct work_struct *work)
  299. {
  300. int ret;
  301. struct zfcp_port *port = container_of(work, struct zfcp_port,
  302. gid_pn_work);
  303. ret = zfcp_fc_ns_gid_pn(port);
  304. if (ret) {
  305. /* could not issue gid_pn for some reason */
  306. zfcp_erp_adapter_reopen(port->adapter, 0, "fcgpn_1");
  307. goto out;
  308. }
  309. if (!port->d_id) {
  310. zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_ERP_FAILED);
  311. goto out;
  312. }
  313. zfcp_erp_port_reopen(port, 0, "fcgpn_3");
  314. out:
  315. put_device(&port->dev);
  316. }
  317. /**
  318. * zfcp_fc_trigger_did_lookup - trigger the d_id lookup using a GID_PN request
  319. * @port: The zfcp_port to lookup the d_id for.
  320. */
  321. void zfcp_fc_trigger_did_lookup(struct zfcp_port *port)
  322. {
  323. get_device(&port->dev);
  324. if (!queue_work(port->adapter->work_queue, &port->gid_pn_work))
  325. put_device(&port->dev);
  326. }
  327. /**
  328. * zfcp_fc_plogi_evaluate - evaluate PLOGI playload
  329. * @port: zfcp_port structure
  330. * @plogi: plogi payload
  331. *
  332. * Evaluate PLOGI playload and copy important fields into zfcp_port structure
  333. */
  334. void zfcp_fc_plogi_evaluate(struct zfcp_port *port, struct fc_els_flogi *plogi)
  335. {
  336. if (plogi->fl_wwpn != port->wwpn) {
  337. port->d_id = 0;
  338. dev_warn(&port->adapter->ccw_device->dev,
  339. "A port opened with WWPN 0x%016Lx returned data that "
  340. "identifies it as WWPN 0x%016Lx\n",
  341. (unsigned long long) port->wwpn,
  342. (unsigned long long) plogi->fl_wwpn);
  343. return;
  344. }
  345. port->wwnn = plogi->fl_wwnn;
  346. port->maxframe_size = plogi->fl_csp.sp_bb_data;
  347. if (plogi->fl_cssp[0].cp_class & FC_CPC_VALID)
  348. port->supported_classes |= FC_COS_CLASS1;
  349. if (plogi->fl_cssp[1].cp_class & FC_CPC_VALID)
  350. port->supported_classes |= FC_COS_CLASS2;
  351. if (plogi->fl_cssp[2].cp_class & FC_CPC_VALID)
  352. port->supported_classes |= FC_COS_CLASS3;
  353. if (plogi->fl_cssp[3].cp_class & FC_CPC_VALID)
  354. port->supported_classes |= FC_COS_CLASS4;
  355. }
  356. static void zfcp_fc_adisc_handler(void *data)
  357. {
  358. struct zfcp_fc_req *fc_req = data;
  359. struct zfcp_port *port = fc_req->ct_els.port;
  360. struct fc_els_adisc *adisc_resp = &fc_req->u.adisc.rsp;
  361. if (fc_req->ct_els.status) {
  362. /* request rejected or timed out */
  363. zfcp_erp_port_forced_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  364. "fcadh_1");
  365. goto out;
  366. }
  367. if (!port->wwnn)
  368. port->wwnn = adisc_resp->adisc_wwnn;
  369. if ((port->wwpn != adisc_resp->adisc_wwpn) ||
  370. !(atomic_read(&port->status) & ZFCP_STATUS_COMMON_OPEN)) {
  371. zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED,
  372. "fcadh_2");
  373. goto out;
  374. }
  375. /* port is good, unblock rport without going through erp */
  376. zfcp_scsi_schedule_rport_register(port);
  377. out:
  378. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  379. put_device(&port->dev);
  380. kmem_cache_free(zfcp_fc_req_cache, fc_req);
  381. }
  382. static int zfcp_fc_adisc(struct zfcp_port *port)
  383. {
  384. struct zfcp_fc_req *fc_req;
  385. struct zfcp_adapter *adapter = port->adapter;
  386. struct Scsi_Host *shost = adapter->scsi_host;
  387. int ret;
  388. fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_ATOMIC);
  389. if (!fc_req)
  390. return -ENOMEM;
  391. fc_req->ct_els.port = port;
  392. fc_req->ct_els.req = &fc_req->sg_req;
  393. fc_req->ct_els.resp = &fc_req->sg_rsp;
  394. sg_init_one(&fc_req->sg_req, &fc_req->u.adisc.req,
  395. sizeof(struct fc_els_adisc));
  396. sg_init_one(&fc_req->sg_rsp, &fc_req->u.adisc.rsp,
  397. sizeof(struct fc_els_adisc));
  398. fc_req->ct_els.handler = zfcp_fc_adisc_handler;
  399. fc_req->ct_els.handler_data = fc_req;
  400. /* acc. to FC-FS, hard_nport_id in ADISC should not be set for ports
  401. without FC-AL-2 capability, so we don't set it */
  402. fc_req->u.adisc.req.adisc_wwpn = fc_host_port_name(shost);
  403. fc_req->u.adisc.req.adisc_wwnn = fc_host_node_name(shost);
  404. fc_req->u.adisc.req.adisc_cmd = ELS_ADISC;
  405. hton24(fc_req->u.adisc.req.adisc_port_id, fc_host_port_id(shost));
  406. ret = zfcp_fsf_send_els(adapter, port->d_id, &fc_req->ct_els,
  407. ZFCP_FC_CTELS_TMO);
  408. if (ret)
  409. kmem_cache_free(zfcp_fc_req_cache, fc_req);
  410. return ret;
  411. }
  412. void zfcp_fc_link_test_work(struct work_struct *work)
  413. {
  414. struct zfcp_port *port =
  415. container_of(work, struct zfcp_port, test_link_work);
  416. int retval;
  417. get_device(&port->dev);
  418. port->rport_task = RPORT_DEL;
  419. zfcp_scsi_rport_work(&port->rport_work);
  420. /* only issue one test command at one time per port */
  421. if (atomic_read(&port->status) & ZFCP_STATUS_PORT_LINK_TEST)
  422. goto out;
  423. atomic_set_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  424. retval = zfcp_fc_adisc(port);
  425. if (retval == 0)
  426. return;
  427. /* send of ADISC was not possible */
  428. atomic_clear_mask(ZFCP_STATUS_PORT_LINK_TEST, &port->status);
  429. zfcp_erp_port_forced_reopen(port, 0, "fcltwk1");
  430. out:
  431. put_device(&port->dev);
  432. }
  433. /**
  434. * zfcp_fc_test_link - lightweight link test procedure
  435. * @port: port to be tested
  436. *
  437. * Test status of a link to a remote port using the ELS command ADISC.
  438. * If there is a problem with the remote port, error recovery steps
  439. * will be triggered.
  440. */
  441. void zfcp_fc_test_link(struct zfcp_port *port)
  442. {
  443. get_device(&port->dev);
  444. if (!queue_work(port->adapter->work_queue, &port->test_link_work))
  445. put_device(&port->dev);
  446. }
  447. static struct zfcp_fc_req *zfcp_alloc_sg_env(int buf_num)
  448. {
  449. struct zfcp_fc_req *fc_req;
  450. fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
  451. if (!fc_req)
  452. return NULL;
  453. if (zfcp_sg_setup_table(&fc_req->sg_rsp, buf_num)) {
  454. kmem_cache_free(zfcp_fc_req_cache, fc_req);
  455. return NULL;
  456. }
  457. sg_init_one(&fc_req->sg_req, &fc_req->u.gpn_ft.req,
  458. sizeof(struct zfcp_fc_gpn_ft_req));
  459. return fc_req;
  460. }
  461. static int zfcp_fc_send_gpn_ft(struct zfcp_fc_req *fc_req,
  462. struct zfcp_adapter *adapter, int max_bytes)
  463. {
  464. struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
  465. struct zfcp_fc_gpn_ft_req *req = &fc_req->u.gpn_ft.req;
  466. DECLARE_COMPLETION_ONSTACK(completion);
  467. int ret;
  468. zfcp_fc_ct_ns_init(&req->ct_hdr, FC_NS_GPN_FT, max_bytes);
  469. req->gpn_ft.fn_fc4_type = FC_TYPE_FCP;
  470. ct_els->handler = zfcp_fc_complete;
  471. ct_els->handler_data = &completion;
  472. ct_els->req = &fc_req->sg_req;
  473. ct_els->resp = &fc_req->sg_rsp;
  474. ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
  475. ZFCP_FC_CTELS_TMO);
  476. if (!ret)
  477. wait_for_completion(&completion);
  478. return ret;
  479. }
  480. static void zfcp_fc_validate_port(struct zfcp_port *port, struct list_head *lh)
  481. {
  482. if (!(atomic_read(&port->status) & ZFCP_STATUS_COMMON_NOESC))
  483. return;
  484. atomic_clear_mask(ZFCP_STATUS_COMMON_NOESC, &port->status);
  485. if ((port->supported_classes != 0) ||
  486. !list_empty(&port->unit_list))
  487. return;
  488. list_move_tail(&port->list, lh);
  489. }
  490. static int zfcp_fc_eval_gpn_ft(struct zfcp_fc_req *fc_req,
  491. struct zfcp_adapter *adapter, int max_entries)
  492. {
  493. struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
  494. struct scatterlist *sg = &fc_req->sg_rsp;
  495. struct fc_ct_hdr *hdr = sg_virt(sg);
  496. struct fc_gpn_ft_resp *acc = sg_virt(sg);
  497. struct zfcp_port *port, *tmp;
  498. unsigned long flags;
  499. LIST_HEAD(remove_lh);
  500. u32 d_id;
  501. int ret = 0, x, last = 0;
  502. if (ct_els->status)
  503. return -EIO;
  504. if (hdr->ct_cmd != FC_FS_ACC) {
  505. if (hdr->ct_reason == FC_BA_RJT_UNABLE)
  506. return -EAGAIN; /* might be a temporary condition */
  507. return -EIO;
  508. }
  509. if (hdr->ct_mr_size) {
  510. dev_warn(&adapter->ccw_device->dev,
  511. "The name server reported %d words residual data\n",
  512. hdr->ct_mr_size);
  513. return -E2BIG;
  514. }
  515. /* first entry is the header */
  516. for (x = 1; x < max_entries && !last; x++) {
  517. if (x % (ZFCP_FC_GPN_FT_ENT_PAGE + 1))
  518. acc++;
  519. else
  520. acc = sg_virt(++sg);
  521. last = acc->fp_flags & FC_NS_FID_LAST;
  522. d_id = ntoh24(acc->fp_fid);
  523. /* don't attach ports with a well known address */
  524. if (d_id >= FC_FID_WELL_KNOWN_BASE)
  525. continue;
  526. /* skip the adapter's port and known remote ports */
  527. if (acc->fp_wwpn == fc_host_port_name(adapter->scsi_host))
  528. continue;
  529. port = zfcp_port_enqueue(adapter, acc->fp_wwpn,
  530. ZFCP_STATUS_COMMON_NOESC, d_id);
  531. if (!IS_ERR(port))
  532. zfcp_erp_port_reopen(port, 0, "fcegpf1");
  533. else if (PTR_ERR(port) != -EEXIST)
  534. ret = PTR_ERR(port);
  535. }
  536. zfcp_erp_wait(adapter);
  537. write_lock_irqsave(&adapter->port_list_lock, flags);
  538. list_for_each_entry_safe(port, tmp, &adapter->port_list, list)
  539. zfcp_fc_validate_port(port, &remove_lh);
  540. write_unlock_irqrestore(&adapter->port_list_lock, flags);
  541. list_for_each_entry_safe(port, tmp, &remove_lh, list) {
  542. zfcp_erp_port_shutdown(port, 0, "fcegpf2");
  543. zfcp_device_unregister(&port->dev, &zfcp_sysfs_port_attrs);
  544. }
  545. return ret;
  546. }
  547. /**
  548. * zfcp_fc_scan_ports - scan remote ports and attach new ports
  549. * @work: reference to scheduled work
  550. */
  551. void zfcp_fc_scan_ports(struct work_struct *work)
  552. {
  553. struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
  554. scan_work);
  555. int ret, i;
  556. struct zfcp_fc_req *fc_req;
  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_FC_GPN_FT_NUM_BUFS : 1;
  560. max_entries = chain ? ZFCP_FC_GPN_FT_MAX_ENT : ZFCP_FC_GPN_FT_ENT_PAGE;
  561. max_bytes = chain ? ZFCP_FC_GPN_FT_MAX_SIZE : ZFCP_FC_CT_SIZE_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;
  565. if (zfcp_fc_wka_port_get(&adapter->gs->ds))
  566. return;
  567. fc_req = zfcp_alloc_sg_env(buf_num);
  568. if (!fc_req)
  569. goto out;
  570. for (i = 0; i < 3; i++) {
  571. ret = zfcp_fc_send_gpn_ft(fc_req, adapter, max_bytes);
  572. if (!ret) {
  573. ret = zfcp_fc_eval_gpn_ft(fc_req, adapter, max_entries);
  574. if (ret == -EAGAIN)
  575. ssleep(1);
  576. else
  577. break;
  578. }
  579. }
  580. zfcp_sg_free_table(&fc_req->sg_rsp, buf_num);
  581. kmem_cache_free(zfcp_fc_req_cache, fc_req);
  582. out:
  583. zfcp_fc_wka_port_put(&adapter->gs->ds);
  584. }
  585. static int zfcp_fc_gspn(struct zfcp_adapter *adapter,
  586. struct zfcp_fc_req *fc_req)
  587. {
  588. DECLARE_COMPLETION_ONSTACK(completion);
  589. char devno[] = "DEVNO:";
  590. struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
  591. struct zfcp_fc_gspn_req *gspn_req = &fc_req->u.gspn.req;
  592. struct zfcp_fc_gspn_rsp *gspn_rsp = &fc_req->u.gspn.rsp;
  593. int ret;
  594. zfcp_fc_ct_ns_init(&gspn_req->ct_hdr, FC_NS_GSPN_ID,
  595. FC_SYMBOLIC_NAME_SIZE);
  596. hton24(gspn_req->gspn.fp_fid, fc_host_port_id(adapter->scsi_host));
  597. sg_init_one(&fc_req->sg_req, gspn_req, sizeof(*gspn_req));
  598. sg_init_one(&fc_req->sg_rsp, gspn_rsp, sizeof(*gspn_rsp));
  599. ct_els->handler = zfcp_fc_complete;
  600. ct_els->handler_data = &completion;
  601. ct_els->req = &fc_req->sg_req;
  602. ct_els->resp = &fc_req->sg_rsp;
  603. ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
  604. ZFCP_FC_CTELS_TMO);
  605. if (ret)
  606. return ret;
  607. wait_for_completion(&completion);
  608. if (ct_els->status)
  609. return ct_els->status;
  610. if (fc_host_port_type(adapter->scsi_host) == FC_PORTTYPE_NPIV &&
  611. !(strstr(gspn_rsp->gspn.fp_name, devno)))
  612. snprintf(fc_host_symbolic_name(adapter->scsi_host),
  613. FC_SYMBOLIC_NAME_SIZE, "%s%s %s NAME: %s",
  614. gspn_rsp->gspn.fp_name, devno,
  615. dev_name(&adapter->ccw_device->dev),
  616. init_utsname()->nodename);
  617. else
  618. strlcpy(fc_host_symbolic_name(adapter->scsi_host),
  619. gspn_rsp->gspn.fp_name, FC_SYMBOLIC_NAME_SIZE);
  620. return 0;
  621. }
  622. static void zfcp_fc_rspn(struct zfcp_adapter *adapter,
  623. struct zfcp_fc_req *fc_req)
  624. {
  625. DECLARE_COMPLETION_ONSTACK(completion);
  626. struct Scsi_Host *shost = adapter->scsi_host;
  627. struct zfcp_fsf_ct_els *ct_els = &fc_req->ct_els;
  628. struct zfcp_fc_rspn_req *rspn_req = &fc_req->u.rspn.req;
  629. struct fc_ct_hdr *rspn_rsp = &fc_req->u.rspn.rsp;
  630. int ret, len;
  631. zfcp_fc_ct_ns_init(&rspn_req->ct_hdr, FC_NS_RSPN_ID,
  632. FC_SYMBOLIC_NAME_SIZE);
  633. hton24(rspn_req->rspn.fr_fid.fp_fid, fc_host_port_id(shost));
  634. len = strlcpy(rspn_req->rspn.fr_name, fc_host_symbolic_name(shost),
  635. FC_SYMBOLIC_NAME_SIZE);
  636. rspn_req->rspn.fr_name_len = len;
  637. sg_init_one(&fc_req->sg_req, rspn_req, sizeof(*rspn_req));
  638. sg_init_one(&fc_req->sg_rsp, rspn_rsp, sizeof(*rspn_rsp));
  639. ct_els->handler = zfcp_fc_complete;
  640. ct_els->handler_data = &completion;
  641. ct_els->req = &fc_req->sg_req;
  642. ct_els->resp = &fc_req->sg_rsp;
  643. ret = zfcp_fsf_send_ct(&adapter->gs->ds, ct_els, NULL,
  644. ZFCP_FC_CTELS_TMO);
  645. if (!ret)
  646. wait_for_completion(&completion);
  647. }
  648. /**
  649. * zfcp_fc_sym_name_update - Retrieve and update the symbolic port name
  650. * @work: ns_up_work of the adapter where to update the symbolic port name
  651. *
  652. * Retrieve the current symbolic port name that may have been set by
  653. * the hardware using the GSPN request and update the fc_host
  654. * symbolic_name sysfs attribute. When running in NPIV mode (and hence
  655. * the port name is unique for this system), update the symbolic port
  656. * name to add Linux specific information and update the FC nameserver
  657. * using the RSPN request.
  658. */
  659. void zfcp_fc_sym_name_update(struct work_struct *work)
  660. {
  661. struct zfcp_adapter *adapter = container_of(work, struct zfcp_adapter,
  662. ns_up_work);
  663. int ret;
  664. struct zfcp_fc_req *fc_req;
  665. if (fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPORT &&
  666. fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
  667. return;
  668. fc_req = kmem_cache_zalloc(zfcp_fc_req_cache, GFP_KERNEL);
  669. if (!fc_req)
  670. return;
  671. ret = zfcp_fc_wka_port_get(&adapter->gs->ds);
  672. if (ret)
  673. goto out_free;
  674. ret = zfcp_fc_gspn(adapter, fc_req);
  675. if (ret || fc_host_port_type(adapter->scsi_host) != FC_PORTTYPE_NPIV)
  676. goto out_ds_put;
  677. memset(fc_req, 0, sizeof(*fc_req));
  678. zfcp_fc_rspn(adapter, fc_req);
  679. out_ds_put:
  680. zfcp_fc_wka_port_put(&adapter->gs->ds);
  681. out_free:
  682. kmem_cache_free(zfcp_fc_req_cache, fc_req);
  683. }
  684. static void zfcp_fc_ct_els_job_handler(void *data)
  685. {
  686. struct fc_bsg_job *job = data;
  687. struct zfcp_fsf_ct_els *zfcp_ct_els = job->dd_data;
  688. struct fc_bsg_reply *jr = job->reply;
  689. jr->reply_payload_rcv_len = job->reply_payload.payload_len;
  690. jr->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
  691. jr->result = zfcp_ct_els->status ? -EIO : 0;
  692. job->job_done(job);
  693. }
  694. static struct zfcp_fc_wka_port *zfcp_fc_job_wka_port(struct fc_bsg_job *job)
  695. {
  696. u32 preamble_word1;
  697. u8 gs_type;
  698. struct zfcp_adapter *adapter;
  699. preamble_word1 = job->request->rqst_data.r_ct.preamble_word1;
  700. gs_type = (preamble_word1 & 0xff000000) >> 24;
  701. adapter = (struct zfcp_adapter *) job->shost->hostdata[0];
  702. switch (gs_type) {
  703. case FC_FST_ALIAS:
  704. return &adapter->gs->as;
  705. case FC_FST_MGMT:
  706. return &adapter->gs->ms;
  707. case FC_FST_TIME:
  708. return &adapter->gs->ts;
  709. break;
  710. case FC_FST_DIR:
  711. return &adapter->gs->ds;
  712. break;
  713. default:
  714. return NULL;
  715. }
  716. }
  717. static void zfcp_fc_ct_job_handler(void *data)
  718. {
  719. struct fc_bsg_job *job = data;
  720. struct zfcp_fc_wka_port *wka_port;
  721. wka_port = zfcp_fc_job_wka_port(job);
  722. zfcp_fc_wka_port_put(wka_port);
  723. zfcp_fc_ct_els_job_handler(data);
  724. }
  725. static int zfcp_fc_exec_els_job(struct fc_bsg_job *job,
  726. struct zfcp_adapter *adapter)
  727. {
  728. struct zfcp_fsf_ct_els *els = job->dd_data;
  729. struct fc_rport *rport = job->rport;
  730. struct zfcp_port *port;
  731. u32 d_id;
  732. if (rport) {
  733. port = zfcp_get_port_by_wwpn(adapter, rport->port_name);
  734. if (!port)
  735. return -EINVAL;
  736. d_id = port->d_id;
  737. put_device(&port->dev);
  738. } else
  739. d_id = ntoh24(job->request->rqst_data.h_els.port_id);
  740. els->handler = zfcp_fc_ct_els_job_handler;
  741. return zfcp_fsf_send_els(adapter, d_id, els, job->req->timeout / HZ);
  742. }
  743. static int zfcp_fc_exec_ct_job(struct fc_bsg_job *job,
  744. struct zfcp_adapter *adapter)
  745. {
  746. int ret;
  747. struct zfcp_fsf_ct_els *ct = job->dd_data;
  748. struct zfcp_fc_wka_port *wka_port;
  749. wka_port = zfcp_fc_job_wka_port(job);
  750. if (!wka_port)
  751. return -EINVAL;
  752. ret = zfcp_fc_wka_port_get(wka_port);
  753. if (ret)
  754. return ret;
  755. ct->handler = zfcp_fc_ct_job_handler;
  756. ret = zfcp_fsf_send_ct(wka_port, ct, NULL, job->req->timeout / HZ);
  757. if (ret)
  758. zfcp_fc_wka_port_put(wka_port);
  759. return ret;
  760. }
  761. int zfcp_fc_exec_bsg_job(struct fc_bsg_job *job)
  762. {
  763. struct Scsi_Host *shost;
  764. struct zfcp_adapter *adapter;
  765. struct zfcp_fsf_ct_els *ct_els = job->dd_data;
  766. shost = job->rport ? rport_to_shost(job->rport) : job->shost;
  767. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  768. if (!(atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_OPEN))
  769. return -EINVAL;
  770. ct_els->req = job->request_payload.sg_list;
  771. ct_els->resp = job->reply_payload.sg_list;
  772. ct_els->handler_data = job;
  773. switch (job->request->msgcode) {
  774. case FC_BSG_RPT_ELS:
  775. case FC_BSG_HST_ELS_NOLOGIN:
  776. return zfcp_fc_exec_els_job(job, adapter);
  777. case FC_BSG_RPT_CT:
  778. case FC_BSG_HST_CT:
  779. return zfcp_fc_exec_ct_job(job, adapter);
  780. default:
  781. return -EINVAL;
  782. }
  783. }
  784. int zfcp_fc_timeout_bsg_job(struct fc_bsg_job *job)
  785. {
  786. /* hardware tracks timeout, reset bsg timeout to not interfere */
  787. return -EAGAIN;
  788. }
  789. int zfcp_fc_gs_setup(struct zfcp_adapter *adapter)
  790. {
  791. struct zfcp_fc_wka_ports *wka_ports;
  792. wka_ports = kzalloc(sizeof(struct zfcp_fc_wka_ports), GFP_KERNEL);
  793. if (!wka_ports)
  794. return -ENOMEM;
  795. adapter->gs = wka_ports;
  796. zfcp_fc_wka_port_init(&wka_ports->ms, FC_FID_MGMT_SERV, adapter);
  797. zfcp_fc_wka_port_init(&wka_ports->ts, FC_FID_TIME_SERV, adapter);
  798. zfcp_fc_wka_port_init(&wka_ports->ds, FC_FID_DIR_SERV, adapter);
  799. zfcp_fc_wka_port_init(&wka_ports->as, FC_FID_ALIASES, adapter);
  800. return 0;
  801. }
  802. void zfcp_fc_gs_destroy(struct zfcp_adapter *adapter)
  803. {
  804. kfree(adapter->gs);
  805. adapter->gs = NULL;
  806. }