dasd_alias.c 28 KB

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