zfcp_fc.c 28 KB

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