dasd_alias.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PAV alias management for the DASD ECKD discipline
  4. *
  5. * Copyright IBM Corp. 2007
  6. * Author(s): Stefan Weinhuber <[email protected]>
  7. */
  8. #define KMSG_COMPONENT "dasd-eckd"
  9. #include <linux/list.h>
  10. #include <linux/slab.h>
  11. #include <asm/ebcdic.h>
  12. #include "dasd_int.h"
  13. #include "dasd_eckd.h"
  14. #ifdef PRINTK_HEADER
  15. #undef PRINTK_HEADER
  16. #endif /* PRINTK_HEADER */
  17. #define PRINTK_HEADER "dasd(eckd):"
  18. /*
  19. * General concept of alias management:
  20. * - PAV and DASD alias management is specific to the eckd discipline.
  21. * - A device is connected to an lcu as long as the device exists.
  22. * dasd_alias_make_device_known_to_lcu will be called wenn the
  23. * device is checked by the eckd discipline and
  24. * dasd_alias_disconnect_device_from_lcu will be called
  25. * before the device is deleted.
  26. * - The dasd_alias_add_device / dasd_alias_remove_device
  27. * functions mark the point when a device is 'ready for service'.
  28. * - A summary unit check is a rare occasion, but it is mandatory to
  29. * support it. It requires some complex recovery actions before the
  30. * devices can be used again (see dasd_alias_handle_summary_unit_check).
  31. * - dasd_alias_get_start_dev will find an alias device that can be used
  32. * instead of the base device and does some (very simple) load balancing.
  33. * This is the function that gets called for each I/O, so when improving
  34. * something, this function should get faster or better, the rest has just
  35. * to be correct.
  36. */
  37. static void summary_unit_check_handling_work(struct work_struct *);
  38. static void lcu_update_work(struct work_struct *);
  39. static int _schedule_lcu_update(struct alias_lcu *, struct dasd_device *);
  40. static struct alias_root aliastree = {
  41. .serverlist = LIST_HEAD_INIT(aliastree.serverlist),
  42. .lock = __SPIN_LOCK_UNLOCKED(aliastree.lock),
  43. };
  44. static struct alias_server *_find_server(struct dasd_uid *uid)
  45. {
  46. struct alias_server *pos;
  47. list_for_each_entry(pos, &aliastree.serverlist, server) {
  48. if (!strncmp(pos->uid.vendor, uid->vendor,
  49. sizeof(uid->vendor))
  50. && !strncmp(pos->uid.serial, uid->serial,
  51. sizeof(uid->serial)))
  52. return pos;
  53. }
  54. return NULL;
  55. }
  56. static struct alias_lcu *_find_lcu(struct alias_server *server,
  57. struct dasd_uid *uid)
  58. {
  59. struct alias_lcu *pos;
  60. list_for_each_entry(pos, &server->lculist, lcu) {
  61. if (pos->uid.ssid == uid->ssid)
  62. return pos;
  63. }
  64. return NULL;
  65. }
  66. static struct alias_pav_group *_find_group(struct alias_lcu *lcu,
  67. struct dasd_uid *uid)
  68. {
  69. struct alias_pav_group *pos;
  70. __u8 search_unit_addr;
  71. /* for hyper pav there is only one group */
  72. if (lcu->pav == HYPER_PAV) {
  73. if (list_empty(&lcu->grouplist))
  74. return NULL;
  75. else
  76. return list_first_entry(&lcu->grouplist,
  77. struct alias_pav_group, group);
  78. }
  79. /* for base pav we have to find the group that matches the base */
  80. if (uid->type == UA_BASE_DEVICE)
  81. search_unit_addr = uid->real_unit_addr;
  82. else
  83. search_unit_addr = uid->base_unit_addr;
  84. list_for_each_entry(pos, &lcu->grouplist, group) {
  85. if (pos->uid.base_unit_addr == search_unit_addr &&
  86. !strncmp(pos->uid.vduit, uid->vduit, sizeof(uid->vduit)))
  87. return pos;
  88. }
  89. return NULL;
  90. }
  91. static struct alias_server *_allocate_server(struct dasd_uid *uid)
  92. {
  93. struct alias_server *server;
  94. server = kzalloc(sizeof(*server), GFP_KERNEL);
  95. if (!server)
  96. return ERR_PTR(-ENOMEM);
  97. memcpy(server->uid.vendor, uid->vendor, sizeof(uid->vendor));
  98. memcpy(server->uid.serial, uid->serial, sizeof(uid->serial));
  99. INIT_LIST_HEAD(&server->server);
  100. INIT_LIST_HEAD(&server->lculist);
  101. return server;
  102. }
  103. static void _free_server(struct alias_server *server)
  104. {
  105. kfree(server);
  106. }
  107. static struct alias_lcu *_allocate_lcu(struct dasd_uid *uid)
  108. {
  109. struct alias_lcu *lcu;
  110. lcu = kzalloc(sizeof(*lcu), GFP_KERNEL);
  111. if (!lcu)
  112. return ERR_PTR(-ENOMEM);
  113. lcu->uac = kzalloc(sizeof(*(lcu->uac)), GFP_KERNEL | GFP_DMA);
  114. if (!lcu->uac)
  115. goto out_err1;
  116. lcu->rsu_cqr = kzalloc(sizeof(*lcu->rsu_cqr), GFP_KERNEL | GFP_DMA);
  117. if (!lcu->rsu_cqr)
  118. goto out_err2;
  119. lcu->rsu_cqr->cpaddr = kzalloc(sizeof(struct ccw1),
  120. GFP_KERNEL | GFP_DMA);
  121. if (!lcu->rsu_cqr->cpaddr)
  122. goto out_err3;
  123. lcu->rsu_cqr->data = kzalloc(16, GFP_KERNEL | GFP_DMA);
  124. if (!lcu->rsu_cqr->data)
  125. goto out_err4;
  126. memcpy(lcu->uid.vendor, uid->vendor, sizeof(uid->vendor));
  127. memcpy(lcu->uid.serial, uid->serial, sizeof(uid->serial));
  128. lcu->uid.ssid = uid->ssid;
  129. lcu->pav = NO_PAV;
  130. lcu->flags = NEED_UAC_UPDATE | UPDATE_PENDING;
  131. INIT_LIST_HEAD(&lcu->lcu);
  132. INIT_LIST_HEAD(&lcu->inactive_devices);
  133. INIT_LIST_HEAD(&lcu->active_devices);
  134. INIT_LIST_HEAD(&lcu->grouplist);
  135. INIT_WORK(&lcu->suc_data.worker, summary_unit_check_handling_work);
  136. INIT_DELAYED_WORK(&lcu->ruac_data.dwork, lcu_update_work);
  137. spin_lock_init(&lcu->lock);
  138. init_completion(&lcu->lcu_setup);
  139. return lcu;
  140. out_err4:
  141. kfree(lcu->rsu_cqr->cpaddr);
  142. out_err3:
  143. kfree(lcu->rsu_cqr);
  144. out_err2:
  145. kfree(lcu->uac);
  146. out_err1:
  147. kfree(lcu);
  148. return ERR_PTR(-ENOMEM);
  149. }
  150. static void _free_lcu(struct alias_lcu *lcu)
  151. {
  152. kfree(lcu->rsu_cqr->data);
  153. kfree(lcu->rsu_cqr->cpaddr);
  154. kfree(lcu->rsu_cqr);
  155. kfree(lcu->uac);
  156. kfree(lcu);
  157. }
  158. /*
  159. * This is the function that will allocate all the server and lcu data,
  160. * so this function must be called first for a new device.
  161. * If the return value is 1, the lcu was already known before, if it
  162. * is 0, this is a new lcu.
  163. * Negative return code indicates that something went wrong (e.g. -ENOMEM)
  164. */
  165. int dasd_alias_make_device_known_to_lcu(struct dasd_device *device)
  166. {
  167. struct dasd_eckd_private *private = device->private;
  168. unsigned long flags;
  169. struct alias_server *server, *newserver;
  170. struct alias_lcu *lcu, *newlcu;
  171. struct dasd_uid uid;
  172. device->discipline->get_uid(device, &uid);
  173. spin_lock_irqsave(&aliastree.lock, flags);
  174. server = _find_server(&uid);
  175. if (!server) {
  176. spin_unlock_irqrestore(&aliastree.lock, flags);
  177. newserver = _allocate_server(&uid);
  178. if (IS_ERR(newserver))
  179. return PTR_ERR(newserver);
  180. spin_lock_irqsave(&aliastree.lock, flags);
  181. server = _find_server(&uid);
  182. if (!server) {
  183. list_add(&newserver->server, &aliastree.serverlist);
  184. server = newserver;
  185. } else {
  186. /* someone was faster */
  187. _free_server(newserver);
  188. }
  189. }
  190. lcu = _find_lcu(server, &uid);
  191. if (!lcu) {
  192. spin_unlock_irqrestore(&aliastree.lock, flags);
  193. newlcu = _allocate_lcu(&uid);
  194. if (IS_ERR(newlcu))
  195. return PTR_ERR(newlcu);
  196. spin_lock_irqsave(&aliastree.lock, flags);
  197. lcu = _find_lcu(server, &uid);
  198. if (!lcu) {
  199. list_add(&newlcu->lcu, &server->lculist);
  200. lcu = newlcu;
  201. } else {
  202. /* someone was faster */
  203. _free_lcu(newlcu);
  204. }
  205. }
  206. spin_lock(&lcu->lock);
  207. list_add(&device->alias_list, &lcu->inactive_devices);
  208. private->lcu = lcu;
  209. spin_unlock(&lcu->lock);
  210. spin_unlock_irqrestore(&aliastree.lock, flags);
  211. return 0;
  212. }
  213. /*
  214. * This function removes a device from the scope of alias management.
  215. * The complicated part is to make sure that it is not in use by
  216. * any of the workers. If necessary cancel the work.
  217. */
  218. void dasd_alias_disconnect_device_from_lcu(struct dasd_device *device)
  219. {
  220. struct dasd_eckd_private *private = device->private;
  221. unsigned long flags;
  222. struct alias_lcu *lcu;
  223. struct alias_server *server;
  224. int was_pending;
  225. struct dasd_uid uid;
  226. lcu = private->lcu;
  227. /* nothing to do if already disconnected */
  228. if (!lcu)
  229. return;
  230. device->discipline->get_uid(device, &uid);
  231. spin_lock_irqsave(&lcu->lock, flags);
  232. /* make sure that the workers don't use this device */
  233. if (device == lcu->suc_data.device) {
  234. spin_unlock_irqrestore(&lcu->lock, flags);
  235. cancel_work_sync(&lcu->suc_data.worker);
  236. spin_lock_irqsave(&lcu->lock, flags);
  237. if (device == lcu->suc_data.device) {
  238. dasd_put_device(device);
  239. lcu->suc_data.device = NULL;
  240. }
  241. }
  242. was_pending = 0;
  243. if (device == lcu->ruac_data.device) {
  244. spin_unlock_irqrestore(&lcu->lock, flags);
  245. was_pending = 1;
  246. cancel_delayed_work_sync(&lcu->ruac_data.dwork);
  247. spin_lock_irqsave(&lcu->lock, flags);
  248. if (device == lcu->ruac_data.device) {
  249. dasd_put_device(device);
  250. lcu->ruac_data.device = NULL;
  251. }
  252. }
  253. private->lcu = NULL;
  254. spin_unlock_irqrestore(&lcu->lock, flags);
  255. spin_lock_irqsave(&aliastree.lock, flags);
  256. spin_lock(&lcu->lock);
  257. list_del_init(&device->alias_list);
  258. if (list_empty(&lcu->grouplist) &&
  259. list_empty(&lcu->active_devices) &&
  260. list_empty(&lcu->inactive_devices)) {
  261. list_del(&lcu->lcu);
  262. spin_unlock(&lcu->lock);
  263. _free_lcu(lcu);
  264. lcu = NULL;
  265. } else {
  266. if (was_pending)
  267. _schedule_lcu_update(lcu, NULL);
  268. spin_unlock(&lcu->lock);
  269. }
  270. server = _find_server(&uid);
  271. if (server && list_empty(&server->lculist)) {
  272. list_del(&server->server);
  273. _free_server(server);
  274. }
  275. spin_unlock_irqrestore(&aliastree.lock, flags);
  276. }
  277. /*
  278. * This function assumes that the unit address configuration stored
  279. * in the lcu is up to date and will update the device uid before
  280. * adding it to a pav group.
  281. */
  282. static int _add_device_to_lcu(struct alias_lcu *lcu,
  283. struct dasd_device *device,
  284. struct dasd_device *pos)
  285. {
  286. struct dasd_eckd_private *private = device->private;
  287. struct alias_pav_group *group;
  288. struct dasd_uid uid;
  289. spin_lock(get_ccwdev_lock(device->cdev));
  290. private->uid.type = lcu->uac->unit[private->uid.real_unit_addr].ua_type;
  291. private->uid.base_unit_addr =
  292. lcu->uac->unit[private->uid.real_unit_addr].base_ua;
  293. uid = private->uid;
  294. spin_unlock(get_ccwdev_lock(device->cdev));
  295. /* if we have no PAV anyway, we don't need to bother with PAV groups */
  296. if (lcu->pav == NO_PAV) {
  297. list_move(&device->alias_list, &lcu->active_devices);
  298. return 0;
  299. }
  300. group = _find_group(lcu, &uid);
  301. if (!group) {
  302. group = kzalloc(sizeof(*group), GFP_ATOMIC);
  303. if (!group)
  304. return -ENOMEM;
  305. memcpy(group->uid.vendor, uid.vendor, sizeof(uid.vendor));
  306. memcpy(group->uid.serial, uid.serial, sizeof(uid.serial));
  307. group->uid.ssid = uid.ssid;
  308. if (uid.type == UA_BASE_DEVICE)
  309. group->uid.base_unit_addr = uid.real_unit_addr;
  310. else
  311. group->uid.base_unit_addr = uid.base_unit_addr;
  312. memcpy(group->uid.vduit, uid.vduit, sizeof(uid.vduit));
  313. INIT_LIST_HEAD(&group->group);
  314. INIT_LIST_HEAD(&group->baselist);
  315. INIT_LIST_HEAD(&group->aliaslist);
  316. list_add(&group->group, &lcu->grouplist);
  317. }
  318. if (uid.type == UA_BASE_DEVICE)
  319. list_move(&device->alias_list, &group->baselist);
  320. else
  321. list_move(&device->alias_list, &group->aliaslist);
  322. private->pavgroup = group;
  323. return 0;
  324. };
  325. static void _remove_device_from_lcu(struct alias_lcu *lcu,
  326. struct dasd_device *device)
  327. {
  328. struct dasd_eckd_private *private = device->private;
  329. struct alias_pav_group *group;
  330. list_move(&device->alias_list, &lcu->inactive_devices);
  331. group = private->pavgroup;
  332. if (!group)
  333. return;
  334. private->pavgroup = NULL;
  335. if (list_empty(&group->baselist) && list_empty(&group->aliaslist)) {
  336. list_del(&group->group);
  337. kfree(group);
  338. return;
  339. }
  340. if (group->next == device)
  341. group->next = NULL;
  342. };
  343. static int
  344. suborder_not_supported(struct dasd_ccw_req *cqr)
  345. {
  346. char *sense;
  347. char reason;
  348. char msg_format;
  349. char msg_no;
  350. /*
  351. * intrc values ENODEV, ENOLINK and EPERM
  352. * will be optained from sleep_on to indicate that no
  353. * IO operation can be started
  354. */
  355. if (cqr->intrc == -ENODEV)
  356. return 1;
  357. if (cqr->intrc == -ENOLINK)
  358. return 1;
  359. if (cqr->intrc == -EPERM)
  360. return 1;
  361. sense = dasd_get_sense(&cqr->irb);
  362. if (!sense)
  363. return 0;
  364. reason = sense[0];
  365. msg_format = (sense[7] & 0xF0);
  366. msg_no = (sense[7] & 0x0F);
  367. /* command reject, Format 0 MSG 4 - invalid parameter */
  368. if ((reason == 0x80) && (msg_format == 0x00) && (msg_no == 0x04))
  369. return 1;
  370. return 0;
  371. }
  372. static int read_unit_address_configuration(struct dasd_device *device,
  373. struct alias_lcu *lcu)
  374. {
  375. struct dasd_psf_prssd_data *prssdp;
  376. struct dasd_ccw_req *cqr;
  377. struct ccw1 *ccw;
  378. int rc;
  379. unsigned long flags;
  380. cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
  381. (sizeof(struct dasd_psf_prssd_data)),
  382. device, NULL);
  383. if (IS_ERR(cqr))
  384. return PTR_ERR(cqr);
  385. cqr->startdev = device;
  386. cqr->memdev = device;
  387. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  388. cqr->retries = 10;
  389. cqr->expires = 20 * HZ;
  390. /* Prepare for Read Subsystem Data */
  391. prssdp = (struct dasd_psf_prssd_data *) cqr->data;
  392. memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
  393. prssdp->order = PSF_ORDER_PRSSD;
  394. prssdp->suborder = 0x0e; /* Read unit address configuration */
  395. /* all other bytes of prssdp must be zero */
  396. ccw = cqr->cpaddr;
  397. ccw->cmd_code = DASD_ECKD_CCW_PSF;
  398. ccw->count = sizeof(struct dasd_psf_prssd_data);
  399. ccw->flags |= CCW_FLAG_CC;
  400. ccw->cda = (__u32)(addr_t) prssdp;
  401. /* Read Subsystem Data - feature codes */
  402. memset(lcu->uac, 0, sizeof(*(lcu->uac)));
  403. ccw++;
  404. ccw->cmd_code = DASD_ECKD_CCW_RSSD;
  405. ccw->count = sizeof(*(lcu->uac));
  406. ccw->cda = (__u32)(addr_t) lcu->uac;
  407. cqr->buildclk = get_tod_clock();
  408. cqr->status = DASD_CQR_FILLED;
  409. /* need to unset flag here to detect race with summary unit check */
  410. spin_lock_irqsave(&lcu->lock, flags);
  411. lcu->flags &= ~NEED_UAC_UPDATE;
  412. spin_unlock_irqrestore(&lcu->lock, flags);
  413. rc = dasd_sleep_on(cqr);
  414. if (!rc)
  415. goto out;
  416. if (suborder_not_supported(cqr)) {
  417. /* suborder not supported or device unusable for IO */
  418. rc = -EOPNOTSUPP;
  419. } else {
  420. /* IO failed but should be retried */
  421. spin_lock_irqsave(&lcu->lock, flags);
  422. lcu->flags |= NEED_UAC_UPDATE;
  423. spin_unlock_irqrestore(&lcu->lock, flags);
  424. }
  425. out:
  426. dasd_sfree_request(cqr, cqr->memdev);
  427. return rc;
  428. }
  429. static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
  430. {
  431. unsigned long flags;
  432. struct alias_pav_group *pavgroup, *tempgroup;
  433. struct dasd_device *device, *tempdev;
  434. int i, rc;
  435. struct dasd_eckd_private *private;
  436. spin_lock_irqsave(&lcu->lock, flags);
  437. list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
  438. list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
  439. alias_list) {
  440. list_move(&device->alias_list, &lcu->active_devices);
  441. private = device->private;
  442. private->pavgroup = NULL;
  443. }
  444. list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
  445. alias_list) {
  446. list_move(&device->alias_list, &lcu->active_devices);
  447. private = device->private;
  448. private->pavgroup = NULL;
  449. }
  450. list_del(&pavgroup->group);
  451. kfree(pavgroup);
  452. }
  453. spin_unlock_irqrestore(&lcu->lock, flags);
  454. rc = read_unit_address_configuration(refdev, lcu);
  455. if (rc)
  456. return rc;
  457. spin_lock_irqsave(&lcu->lock, flags);
  458. /*
  459. * there is another update needed skip the remaining handling
  460. * the data might already be outdated
  461. * but especially do not add the device to an LCU with pending
  462. * update
  463. */
  464. if (lcu->flags & NEED_UAC_UPDATE)
  465. goto out;
  466. lcu->pav = NO_PAV;
  467. for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
  468. switch (lcu->uac->unit[i].ua_type) {
  469. case UA_BASE_PAV_ALIAS:
  470. lcu->pav = BASE_PAV;
  471. break;
  472. case UA_HYPER_PAV_ALIAS:
  473. lcu->pav = HYPER_PAV;
  474. break;
  475. }
  476. if (lcu->pav != NO_PAV)
  477. break;
  478. }
  479. list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
  480. alias_list) {
  481. _add_device_to_lcu(lcu, device, refdev);
  482. }
  483. out:
  484. spin_unlock_irqrestore(&lcu->lock, flags);
  485. return 0;
  486. }
  487. static void lcu_update_work(struct work_struct *work)
  488. {
  489. struct alias_lcu *lcu;
  490. struct read_uac_work_data *ruac_data;
  491. struct dasd_device *device;
  492. unsigned long flags;
  493. int rc;
  494. ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
  495. lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
  496. device = ruac_data->device;
  497. rc = _lcu_update(device, lcu);
  498. /*
  499. * Need to check flags again, as there could have been another
  500. * prepare_update or a new device a new device while we were still
  501. * processing the data
  502. */
  503. spin_lock_irqsave(&lcu->lock, flags);
  504. if ((rc && (rc != -EOPNOTSUPP)) || (lcu->flags & NEED_UAC_UPDATE)) {
  505. DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
  506. " alias data in lcu (rc = %d), retry later", rc);
  507. if (!schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ))
  508. dasd_put_device(device);
  509. } else {
  510. dasd_put_device(device);
  511. lcu->ruac_data.device = NULL;
  512. lcu->flags &= ~UPDATE_PENDING;
  513. }
  514. spin_unlock_irqrestore(&lcu->lock, flags);
  515. }
  516. static int _schedule_lcu_update(struct alias_lcu *lcu,
  517. struct dasd_device *device)
  518. {
  519. struct dasd_device *usedev = NULL;
  520. struct alias_pav_group *group;
  521. lcu->flags |= NEED_UAC_UPDATE;
  522. if (lcu->ruac_data.device) {
  523. /* already scheduled or running */
  524. return 0;
  525. }
  526. if (device && !list_empty(&device->alias_list))
  527. usedev = device;
  528. if (!usedev && !list_empty(&lcu->grouplist)) {
  529. group = list_first_entry(&lcu->grouplist,
  530. struct alias_pav_group, group);
  531. if (!list_empty(&group->baselist))
  532. usedev = list_first_entry(&group->baselist,
  533. struct dasd_device,
  534. alias_list);
  535. else if (!list_empty(&group->aliaslist))
  536. usedev = list_first_entry(&group->aliaslist,
  537. struct dasd_device,
  538. alias_list);
  539. }
  540. if (!usedev && !list_empty(&lcu->active_devices)) {
  541. usedev = list_first_entry(&lcu->active_devices,
  542. struct dasd_device, alias_list);
  543. }
  544. /*
  545. * if we haven't found a proper device yet, give up for now, the next
  546. * device that will be set active will trigger an lcu update
  547. */
  548. if (!usedev)
  549. return -EINVAL;
  550. dasd_get_device(usedev);
  551. lcu->ruac_data.device = usedev;
  552. if (!schedule_delayed_work(&lcu->ruac_data.dwork, 0))
  553. dasd_put_device(usedev);
  554. return 0;
  555. }
  556. int dasd_alias_add_device(struct dasd_device *device)
  557. {
  558. struct dasd_eckd_private *private = device->private;
  559. __u8 uaddr = private->uid.real_unit_addr;
  560. struct alias_lcu *lcu = private->lcu;
  561. unsigned long flags;
  562. int rc;
  563. rc = 0;
  564. spin_lock_irqsave(&lcu->lock, flags);
  565. /*
  566. * Check if device and lcu type differ. If so, the uac data may be
  567. * outdated and needs to be updated.
  568. */
  569. if (private->uid.type != lcu->uac->unit[uaddr].ua_type) {
  570. lcu->flags |= UPDATE_PENDING;
  571. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  572. "uid type mismatch - trigger rescan");
  573. }
  574. if (!(lcu->flags & UPDATE_PENDING)) {
  575. rc = _add_device_to_lcu(lcu, device, device);
  576. if (rc)
  577. lcu->flags |= UPDATE_PENDING;
  578. }
  579. if (lcu->flags & UPDATE_PENDING) {
  580. list_move(&device->alias_list, &lcu->active_devices);
  581. private->pavgroup = NULL;
  582. _schedule_lcu_update(lcu, device);
  583. }
  584. spin_unlock_irqrestore(&lcu->lock, flags);
  585. return rc;
  586. }
  587. int dasd_alias_update_add_device(struct dasd_device *device)
  588. {
  589. struct dasd_eckd_private *private = device->private;
  590. private->lcu->flags |= UPDATE_PENDING;
  591. return dasd_alias_add_device(device);
  592. }
  593. int dasd_alias_remove_device(struct dasd_device *device)
  594. {
  595. struct dasd_eckd_private *private = device->private;
  596. struct alias_lcu *lcu = private->lcu;
  597. unsigned long flags;
  598. /* nothing to do if already removed */
  599. if (!lcu)
  600. return 0;
  601. spin_lock_irqsave(&lcu->lock, flags);
  602. _remove_device_from_lcu(lcu, device);
  603. spin_unlock_irqrestore(&lcu->lock, flags);
  604. return 0;
  605. }
  606. struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
  607. {
  608. struct dasd_eckd_private *alias_priv, *private = base_device->private;
  609. struct alias_lcu *lcu = private->lcu;
  610. struct dasd_device *alias_device;
  611. struct alias_pav_group *group;
  612. unsigned long flags;
  613. if (!lcu)
  614. return NULL;
  615. if (lcu->pav == NO_PAV ||
  616. lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
  617. return NULL;
  618. if (unlikely(!(private->features.feature[8] & 0x01))) {
  619. /*
  620. * PAV enabled but prefix not, very unlikely
  621. * seems to be a lost pathgroup
  622. * use base device to do IO
  623. */
  624. DBF_DEV_EVENT(DBF_ERR, base_device, "%s",
  625. "Prefix not enabled with PAV enabled\n");
  626. return NULL;
  627. }
  628. spin_lock_irqsave(&lcu->lock, flags);
  629. group = private->pavgroup;
  630. if (!group) {
  631. spin_unlock_irqrestore(&lcu->lock, flags);
  632. return NULL;
  633. }
  634. alias_device = group->next;
  635. if (!alias_device) {
  636. if (list_empty(&group->aliaslist)) {
  637. spin_unlock_irqrestore(&lcu->lock, flags);
  638. return NULL;
  639. } else {
  640. alias_device = list_first_entry(&group->aliaslist,
  641. struct dasd_device,
  642. alias_list);
  643. }
  644. }
  645. if (list_is_last(&alias_device->alias_list, &group->aliaslist))
  646. group->next = list_first_entry(&group->aliaslist,
  647. struct dasd_device, alias_list);
  648. else
  649. group->next = list_first_entry(&alias_device->alias_list,
  650. struct dasd_device, alias_list);
  651. spin_unlock_irqrestore(&lcu->lock, flags);
  652. alias_priv = alias_device->private;
  653. if ((alias_priv->count < private->count) && !alias_device->stopped &&
  654. !test_bit(DASD_FLAG_OFFLINE, &alias_device->flags))
  655. return alias_device;
  656. else
  657. return NULL;
  658. }
  659. /*
  660. * Summary unit check handling depends on the way alias devices
  661. * are handled so it is done here rather then in dasd_eckd.c
  662. */
  663. static int reset_summary_unit_check(struct alias_lcu *lcu,
  664. struct dasd_device *device,
  665. char reason)
  666. {
  667. struct dasd_ccw_req *cqr;
  668. int rc = 0;
  669. struct ccw1 *ccw;
  670. cqr = lcu->rsu_cqr;
  671. memcpy((char *) &cqr->magic, "ECKD", 4);
  672. ASCEBC((char *) &cqr->magic, 4);
  673. ccw = cqr->cpaddr;
  674. ccw->cmd_code = DASD_ECKD_CCW_RSCK;
  675. ccw->flags = CCW_FLAG_SLI;
  676. ccw->count = 16;
  677. ccw->cda = (__u32)(addr_t) cqr->data;
  678. ((char *)cqr->data)[0] = reason;
  679. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  680. cqr->retries = 255; /* set retry counter to enable basic ERP */
  681. cqr->startdev = device;
  682. cqr->memdev = device;
  683. cqr->block = NULL;
  684. cqr->expires = 5 * HZ;
  685. cqr->buildclk = get_tod_clock();
  686. cqr->status = DASD_CQR_FILLED;
  687. rc = dasd_sleep_on_immediatly(cqr);
  688. return rc;
  689. }
  690. static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
  691. {
  692. struct alias_pav_group *pavgroup;
  693. struct dasd_device *device;
  694. struct dasd_eckd_private *private;
  695. /* active and inactive list can contain alias as well as base devices */
  696. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  697. private = device->private;
  698. if (private->uid.type != UA_BASE_DEVICE)
  699. continue;
  700. dasd_schedule_block_bh(device->block);
  701. dasd_schedule_device_bh(device);
  702. }
  703. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  704. private = device->private;
  705. if (private->uid.type != UA_BASE_DEVICE)
  706. continue;
  707. dasd_schedule_block_bh(device->block);
  708. dasd_schedule_device_bh(device);
  709. }
  710. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  711. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  712. dasd_schedule_block_bh(device->block);
  713. dasd_schedule_device_bh(device);
  714. }
  715. }
  716. }
  717. static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
  718. {
  719. struct alias_pav_group *pavgroup;
  720. struct dasd_device *device, *temp;
  721. struct dasd_eckd_private *private;
  722. unsigned long flags;
  723. LIST_HEAD(active);
  724. /*
  725. * Problem here ist that dasd_flush_device_queue may wait
  726. * for termination of a request to complete. We can't keep
  727. * the lcu lock during that time, so we must assume that
  728. * the lists may have changed.
  729. * Idea: first gather all active alias devices in a separate list,
  730. * then flush the first element of this list unlocked, and afterwards
  731. * check if it is still on the list before moving it to the
  732. * active_devices list.
  733. */
  734. spin_lock_irqsave(&lcu->lock, flags);
  735. list_for_each_entry_safe(device, temp, &lcu->active_devices,
  736. alias_list) {
  737. private = device->private;
  738. if (private->uid.type == UA_BASE_DEVICE)
  739. continue;
  740. list_move(&device->alias_list, &active);
  741. }
  742. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  743. list_splice_init(&pavgroup->aliaslist, &active);
  744. }
  745. while (!list_empty(&active)) {
  746. device = list_first_entry(&active, struct dasd_device,
  747. alias_list);
  748. spin_unlock_irqrestore(&lcu->lock, flags);
  749. dasd_flush_device_queue(device);
  750. spin_lock_irqsave(&lcu->lock, flags);
  751. /*
  752. * only move device around if it wasn't moved away while we
  753. * were waiting for the flush
  754. */
  755. if (device == list_first_entry(&active,
  756. struct dasd_device, alias_list)) {
  757. list_move(&device->alias_list, &lcu->active_devices);
  758. private = device->private;
  759. private->pavgroup = NULL;
  760. }
  761. }
  762. spin_unlock_irqrestore(&lcu->lock, flags);
  763. }
  764. static void _stop_all_devices_on_lcu(struct alias_lcu *lcu)
  765. {
  766. struct alias_pav_group *pavgroup;
  767. struct dasd_device *device;
  768. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  769. spin_lock(get_ccwdev_lock(device->cdev));
  770. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  771. spin_unlock(get_ccwdev_lock(device->cdev));
  772. }
  773. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  774. spin_lock(get_ccwdev_lock(device->cdev));
  775. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  776. spin_unlock(get_ccwdev_lock(device->cdev));
  777. }
  778. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  779. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  780. spin_lock(get_ccwdev_lock(device->cdev));
  781. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  782. spin_unlock(get_ccwdev_lock(device->cdev));
  783. }
  784. list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
  785. spin_lock(get_ccwdev_lock(device->cdev));
  786. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  787. spin_unlock(get_ccwdev_lock(device->cdev));
  788. }
  789. }
  790. }
  791. static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
  792. {
  793. struct alias_pav_group *pavgroup;
  794. struct dasd_device *device;
  795. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  796. spin_lock(get_ccwdev_lock(device->cdev));
  797. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  798. spin_unlock(get_ccwdev_lock(device->cdev));
  799. }
  800. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  801. spin_lock(get_ccwdev_lock(device->cdev));
  802. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  803. spin_unlock(get_ccwdev_lock(device->cdev));
  804. }
  805. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  806. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  807. spin_lock(get_ccwdev_lock(device->cdev));
  808. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  809. spin_unlock(get_ccwdev_lock(device->cdev));
  810. }
  811. list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
  812. spin_lock(get_ccwdev_lock(device->cdev));
  813. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  814. spin_unlock(get_ccwdev_lock(device->cdev));
  815. }
  816. }
  817. }
  818. static void summary_unit_check_handling_work(struct work_struct *work)
  819. {
  820. struct alias_lcu *lcu;
  821. struct summary_unit_check_work_data *suc_data;
  822. unsigned long flags;
  823. struct dasd_device *device;
  824. suc_data = container_of(work, struct summary_unit_check_work_data,
  825. worker);
  826. lcu = container_of(suc_data, struct alias_lcu, suc_data);
  827. device = suc_data->device;
  828. /* 1. flush alias devices */
  829. flush_all_alias_devices_on_lcu(lcu);
  830. /* 2. reset summary unit check */
  831. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  832. dasd_device_remove_stop_bits(device,
  833. (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
  834. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  835. reset_summary_unit_check(lcu, device, suc_data->reason);
  836. spin_lock_irqsave(&lcu->lock, flags);
  837. _unstop_all_devices_on_lcu(lcu);
  838. _restart_all_base_devices_on_lcu(lcu);
  839. /* 3. read new alias configuration */
  840. _schedule_lcu_update(lcu, device);
  841. lcu->suc_data.device = NULL;
  842. dasd_put_device(device);
  843. spin_unlock_irqrestore(&lcu->lock, flags);
  844. }
  845. void dasd_alias_handle_summary_unit_check(struct work_struct *work)
  846. {
  847. struct dasd_device *device = container_of(work, struct dasd_device,
  848. suc_work);
  849. struct dasd_eckd_private *private = device->private;
  850. struct alias_lcu *lcu;
  851. unsigned long flags;
  852. lcu = private->lcu;
  853. if (!lcu) {
  854. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  855. "device not ready to handle summary"
  856. " unit check (no lcu structure)");
  857. goto out;
  858. }
  859. spin_lock_irqsave(&lcu->lock, flags);
  860. /* If this device is about to be removed just return and wait for
  861. * the next interrupt on a different device
  862. */
  863. if (list_empty(&device->alias_list)) {
  864. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  865. "device is in offline processing,"
  866. " don't do summary unit check handling");
  867. goto out_unlock;
  868. }
  869. if (lcu->suc_data.device) {
  870. /* already scheduled or running */
  871. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  872. "previous instance of summary unit check worker"
  873. " still pending");
  874. goto out_unlock;
  875. }
  876. _stop_all_devices_on_lcu(lcu);
  877. /* prepare for lcu_update */
  878. lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
  879. lcu->suc_data.reason = private->suc_reason;
  880. lcu->suc_data.device = device;
  881. dasd_get_device(device);
  882. if (!schedule_work(&lcu->suc_data.worker))
  883. dasd_put_device(device);
  884. out_unlock:
  885. spin_unlock_irqrestore(&lcu->lock, flags);
  886. out:
  887. clear_bit(DASD_FLAG_SUC, &device->flags);
  888. dasd_put_device(device);
  889. };