zfcp_fc.c 29 KB

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