zfcp_aux.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * Driver authors:
  23. * Martin Peschke (originator of the driver)
  24. * Raimund Schroeder
  25. * Aron Zeh
  26. * Wolfgang Taphorn
  27. * Stefan Bader
  28. * Heiko Carstens (kernel 2.6 port of the driver)
  29. * Andreas Herrmann
  30. * Maxim Shchetynin
  31. * Volker Sameske
  32. * Ralph Wuerthner
  33. */
  34. #include "zfcp_ext.h"
  35. /* accumulated log level (module parameter) */
  36. static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
  37. static char *device;
  38. /*********************** FUNCTION PROTOTYPES *********************************/
  39. /* written against the module interface */
  40. static int __init zfcp_module_init(void);
  41. /* FCP related */
  42. static void zfcp_ns_gid_pn_handler(unsigned long);
  43. /* miscellaneous */
  44. static inline int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
  45. static inline void zfcp_sg_list_free(struct zfcp_sg_list *);
  46. static inline int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
  47. void __user *, size_t);
  48. static inline int zfcp_sg_list_copy_to_user(void __user *,
  49. struct zfcp_sg_list *, size_t);
  50. static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
  51. #define ZFCP_CFDC_IOC_MAGIC 0xDD
  52. #define ZFCP_CFDC_IOC \
  53. _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
  54. static struct file_operations zfcp_cfdc_fops = {
  55. .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
  56. #ifdef CONFIG_COMPAT
  57. .compat_ioctl = zfcp_cfdc_dev_ioctl
  58. #endif
  59. };
  60. static struct miscdevice zfcp_cfdc_misc = {
  61. .minor = ZFCP_CFDC_DEV_MINOR,
  62. .name = ZFCP_CFDC_DEV_NAME,
  63. .fops = &zfcp_cfdc_fops
  64. };
  65. /*********************** KERNEL/MODULE PARAMETERS ***************************/
  66. /* declare driver module init/cleanup functions */
  67. module_init(zfcp_module_init);
  68. MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
  69. MODULE_DESCRIPTION
  70. ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
  71. MODULE_LICENSE("GPL");
  72. module_param(device, charp, 0400);
  73. MODULE_PARM_DESC(device, "specify initial device");
  74. module_param(loglevel, uint, 0400);
  75. MODULE_PARM_DESC(loglevel,
  76. "log levels, 8 nibbles: "
  77. "FC ERP QDIO CIO Config FSF SCSI Other, "
  78. "levels: 0=none 1=normal 2=devel 3=trace");
  79. /****************************************************************/
  80. /************** Functions without logging ***********************/
  81. /****************************************************************/
  82. void
  83. _zfcp_hex_dump(char *addr, int count)
  84. {
  85. int i;
  86. for (i = 0; i < count; i++) {
  87. printk("%02x", addr[i]);
  88. if ((i % 4) == 3)
  89. printk(" ");
  90. if ((i % 32) == 31)
  91. printk("\n");
  92. }
  93. if (((i-1) % 32) != 31)
  94. printk("\n");
  95. }
  96. /****************************************************************/
  97. /****** Functions to handle the request ID hash table ********/
  98. /****************************************************************/
  99. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
  100. static int zfcp_reqlist_init(struct zfcp_adapter *adapter)
  101. {
  102. int i;
  103. adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
  104. GFP_KERNEL);
  105. if (!adapter->req_list)
  106. return -ENOMEM;
  107. for (i=0; i<REQUEST_LIST_SIZE; i++)
  108. INIT_LIST_HEAD(&adapter->req_list[i]);
  109. return 0;
  110. }
  111. static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
  112. {
  113. struct zfcp_fsf_req *request, *tmp;
  114. unsigned int i;
  115. for (i=0; i<REQUEST_LIST_SIZE; i++) {
  116. if (list_empty(&adapter->req_list[i]))
  117. continue;
  118. list_for_each_entry_safe(request, tmp,
  119. &adapter->req_list[i], list)
  120. list_del(&request->list);
  121. }
  122. kfree(adapter->req_list);
  123. }
  124. void zfcp_reqlist_add(struct zfcp_adapter *adapter,
  125. struct zfcp_fsf_req *fsf_req)
  126. {
  127. unsigned int i;
  128. i = fsf_req->req_id % REQUEST_LIST_SIZE;
  129. list_add_tail(&fsf_req->list, &adapter->req_list[i]);
  130. }
  131. void zfcp_reqlist_remove(struct zfcp_adapter *adapter, unsigned long req_id)
  132. {
  133. struct zfcp_fsf_req *request, *tmp;
  134. unsigned int i, counter;
  135. u64 dbg_tmp[2];
  136. i = req_id % REQUEST_LIST_SIZE;
  137. BUG_ON(list_empty(&adapter->req_list[i]));
  138. counter = 0;
  139. list_for_each_entry_safe(request, tmp, &adapter->req_list[i], list) {
  140. if (request->req_id == req_id) {
  141. dbg_tmp[0] = (u64) atomic_read(&adapter->reqs_active);
  142. dbg_tmp[1] = (u64) counter;
  143. debug_event(adapter->erp_dbf, 4, (void *) dbg_tmp, 16);
  144. list_del(&request->list);
  145. break;
  146. }
  147. counter++;
  148. }
  149. }
  150. struct zfcp_fsf_req *zfcp_reqlist_ismember(struct zfcp_adapter *adapter,
  151. unsigned long req_id)
  152. {
  153. struct zfcp_fsf_req *request, *tmp;
  154. unsigned int i;
  155. /* 0 is reserved as an invalid req_id */
  156. if (req_id == 0)
  157. return NULL;
  158. i = req_id % REQUEST_LIST_SIZE;
  159. list_for_each_entry_safe(request, tmp, &adapter->req_list[i], list)
  160. if (request->req_id == req_id)
  161. return request;
  162. return NULL;
  163. }
  164. int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
  165. {
  166. unsigned int i;
  167. for (i=0; i<REQUEST_LIST_SIZE; i++)
  168. if (!list_empty(&adapter->req_list[i]))
  169. return 0;
  170. return 1;
  171. }
  172. #undef ZFCP_LOG_AREA
  173. /****************************************************************/
  174. /************** Uncategorised Functions *************************/
  175. /****************************************************************/
  176. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
  177. /**
  178. * zfcp_device_setup - setup function
  179. * @str: pointer to parameter string
  180. *
  181. * Parse "device=..." parameter string.
  182. */
  183. static int __init
  184. zfcp_device_setup(char *devstr)
  185. {
  186. char *tmp, *str;
  187. size_t len;
  188. if (!devstr)
  189. return 0;
  190. len = strlen(devstr) + 1;
  191. str = (char *) kmalloc(len, GFP_KERNEL);
  192. if (!str)
  193. goto err_out;
  194. memcpy(str, devstr, len);
  195. tmp = strchr(str, ',');
  196. if (!tmp)
  197. goto err_out;
  198. *tmp++ = '\0';
  199. strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
  200. zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
  201. zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
  202. if (*tmp++ != ',')
  203. goto err_out;
  204. if (*tmp == '\0')
  205. goto err_out;
  206. zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
  207. if (*tmp != '\0')
  208. goto err_out;
  209. kfree(str);
  210. return 1;
  211. err_out:
  212. ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
  213. kfree(str);
  214. return 0;
  215. }
  216. static void __init
  217. zfcp_init_device_configure(void)
  218. {
  219. struct zfcp_adapter *adapter;
  220. struct zfcp_port *port;
  221. struct zfcp_unit *unit;
  222. down(&zfcp_data.config_sema);
  223. read_lock_irq(&zfcp_data.config_lock);
  224. adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
  225. if (adapter)
  226. zfcp_adapter_get(adapter);
  227. read_unlock_irq(&zfcp_data.config_lock);
  228. if (adapter == NULL)
  229. goto out_adapter;
  230. port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
  231. if (!port)
  232. goto out_port;
  233. unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
  234. if (!unit)
  235. goto out_unit;
  236. up(&zfcp_data.config_sema);
  237. ccw_device_set_online(adapter->ccw_device);
  238. zfcp_erp_wait(adapter);
  239. down(&zfcp_data.config_sema);
  240. zfcp_unit_put(unit);
  241. out_unit:
  242. zfcp_port_put(port);
  243. out_port:
  244. zfcp_adapter_put(adapter);
  245. out_adapter:
  246. up(&zfcp_data.config_sema);
  247. return;
  248. }
  249. static int calc_alignment(int size)
  250. {
  251. int align = 1;
  252. if (!size)
  253. return 0;
  254. while ((size - align) > 0)
  255. align <<= 1;
  256. return align;
  257. }
  258. static int __init
  259. zfcp_module_init(void)
  260. {
  261. int retval = -ENOMEM;
  262. int size, align;
  263. size = sizeof(struct zfcp_fsf_req_qtcb);
  264. align = calc_alignment(size);
  265. zfcp_data.fsf_req_qtcb_cache =
  266. kmem_cache_create("zfcp_fsf", size, align, 0, NULL, NULL);
  267. if (!zfcp_data.fsf_req_qtcb_cache)
  268. goto out;
  269. size = sizeof(struct fsf_status_read_buffer);
  270. align = calc_alignment(size);
  271. zfcp_data.sr_buffer_cache =
  272. kmem_cache_create("zfcp_sr", size, align, 0, NULL, NULL);
  273. if (!zfcp_data.sr_buffer_cache)
  274. goto out_sr_cache;
  275. size = sizeof(struct zfcp_gid_pn_data);
  276. align = calc_alignment(size);
  277. zfcp_data.gid_pn_cache =
  278. kmem_cache_create("zfcp_gid", size, align, 0, NULL, NULL);
  279. if (!zfcp_data.gid_pn_cache)
  280. goto out_gid_cache;
  281. atomic_set(&zfcp_data.loglevel, loglevel);
  282. /* initialize adapter list */
  283. INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
  284. /* initialize adapters to be removed list head */
  285. INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
  286. zfcp_data.scsi_transport_template =
  287. fc_attach_transport(&zfcp_transport_functions);
  288. if (!zfcp_data.scsi_transport_template)
  289. goto out_transport;
  290. retval = misc_register(&zfcp_cfdc_misc);
  291. if (retval != 0) {
  292. ZFCP_LOG_INFO("registration of misc device "
  293. "zfcp_cfdc failed\n");
  294. goto out_misc;
  295. }
  296. ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
  297. ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
  298. /* Initialise proc semaphores */
  299. sema_init(&zfcp_data.config_sema, 1);
  300. /* initialise configuration rw lock */
  301. rwlock_init(&zfcp_data.config_lock);
  302. /* setup dynamic I/O */
  303. retval = zfcp_ccw_register();
  304. if (retval) {
  305. ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
  306. goto out_ccw_register;
  307. }
  308. if (zfcp_device_setup(device))
  309. zfcp_init_device_configure();
  310. goto out;
  311. out_ccw_register:
  312. misc_deregister(&zfcp_cfdc_misc);
  313. out_misc:
  314. fc_release_transport(zfcp_data.scsi_transport_template);
  315. out_transport:
  316. kmem_cache_destroy(zfcp_data.gid_pn_cache);
  317. out_gid_cache:
  318. kmem_cache_destroy(zfcp_data.sr_buffer_cache);
  319. out_sr_cache:
  320. kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
  321. out:
  322. return retval;
  323. }
  324. /*
  325. * function: zfcp_cfdc_dev_ioctl
  326. *
  327. * purpose: Handle control file upload/download transaction via IOCTL
  328. * interface
  329. *
  330. * returns: 0 - Operation completed successfuly
  331. * -ENOTTY - Unknown IOCTL command
  332. * -EINVAL - Invalid sense data record
  333. * -ENXIO - The FCP adapter is not available
  334. * -EOPNOTSUPP - The FCP adapter does not have CFDC support
  335. * -ENOMEM - Insufficient memory
  336. * -EFAULT - User space memory I/O operation fault
  337. * -EPERM - Cannot create or queue FSF request or create SBALs
  338. * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
  339. */
  340. static long
  341. zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
  342. unsigned long buffer)
  343. {
  344. struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
  345. struct zfcp_adapter *adapter = NULL;
  346. struct zfcp_fsf_req *fsf_req = NULL;
  347. struct zfcp_sg_list *sg_list = NULL;
  348. u32 fsf_command, option;
  349. char *bus_id = NULL;
  350. int retval = 0;
  351. sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
  352. if (sense_data == NULL) {
  353. retval = -ENOMEM;
  354. goto out;
  355. }
  356. sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
  357. if (sg_list == NULL) {
  358. retval = -ENOMEM;
  359. goto out;
  360. }
  361. if (command != ZFCP_CFDC_IOC) {
  362. ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
  363. retval = -ENOTTY;
  364. goto out;
  365. }
  366. if ((sense_data_user = (void __user *) buffer) == NULL) {
  367. ZFCP_LOG_INFO("sense data record is required\n");
  368. retval = -EINVAL;
  369. goto out;
  370. }
  371. retval = copy_from_user(sense_data, sense_data_user,
  372. sizeof(struct zfcp_cfdc_sense_data));
  373. if (retval) {
  374. retval = -EFAULT;
  375. goto out;
  376. }
  377. if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
  378. ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
  379. ZFCP_CFDC_SIGNATURE);
  380. retval = -EINVAL;
  381. goto out;
  382. }
  383. switch (sense_data->command) {
  384. case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
  385. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  386. option = FSF_CFDC_OPTION_NORMAL_MODE;
  387. break;
  388. case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
  389. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  390. option = FSF_CFDC_OPTION_FORCE;
  391. break;
  392. case ZFCP_CFDC_CMND_FULL_ACCESS:
  393. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  394. option = FSF_CFDC_OPTION_FULL_ACCESS;
  395. break;
  396. case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
  397. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  398. option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
  399. break;
  400. case ZFCP_CFDC_CMND_UPLOAD:
  401. fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
  402. option = 0;
  403. break;
  404. default:
  405. ZFCP_LOG_INFO("invalid command code 0x%08x\n",
  406. sense_data->command);
  407. retval = -EINVAL;
  408. goto out;
  409. }
  410. bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  411. if (bus_id == NULL) {
  412. retval = -ENOMEM;
  413. goto out;
  414. }
  415. snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
  416. (sense_data->devno >> 24),
  417. (sense_data->devno >> 16) & 0xFF,
  418. (sense_data->devno & 0xFFFF));
  419. read_lock_irq(&zfcp_data.config_lock);
  420. adapter = zfcp_get_adapter_by_busid(bus_id);
  421. if (adapter)
  422. zfcp_adapter_get(adapter);
  423. read_unlock_irq(&zfcp_data.config_lock);
  424. kfree(bus_id);
  425. if (adapter == NULL) {
  426. ZFCP_LOG_INFO("invalid adapter\n");
  427. retval = -ENXIO;
  428. goto out;
  429. }
  430. if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
  431. retval = zfcp_sg_list_alloc(sg_list,
  432. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  433. if (retval) {
  434. retval = -ENOMEM;
  435. goto out;
  436. }
  437. }
  438. if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
  439. (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
  440. retval = zfcp_sg_list_copy_from_user(
  441. sg_list, &sense_data_user->control_file,
  442. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  443. if (retval) {
  444. retval = -EFAULT;
  445. goto out;
  446. }
  447. }
  448. retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
  449. option, sg_list);
  450. if (retval)
  451. goto out;
  452. if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
  453. (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
  454. retval = -ENXIO;
  455. goto out;
  456. }
  457. sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
  458. memcpy(&sense_data->fsf_status_qual,
  459. &fsf_req->qtcb->header.fsf_status_qual,
  460. sizeof(union fsf_status_qual));
  461. memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
  462. retval = copy_to_user(sense_data_user, sense_data,
  463. sizeof(struct zfcp_cfdc_sense_data));
  464. if (retval) {
  465. retval = -EFAULT;
  466. goto out;
  467. }
  468. if (sense_data->command & ZFCP_CFDC_UPLOAD) {
  469. retval = zfcp_sg_list_copy_to_user(
  470. &sense_data_user->control_file, sg_list,
  471. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  472. if (retval) {
  473. retval = -EFAULT;
  474. goto out;
  475. }
  476. }
  477. out:
  478. if (fsf_req != NULL)
  479. zfcp_fsf_req_free(fsf_req);
  480. if ((adapter != NULL) && (retval != -ENXIO))
  481. zfcp_adapter_put(adapter);
  482. if (sg_list != NULL) {
  483. zfcp_sg_list_free(sg_list);
  484. kfree(sg_list);
  485. }
  486. kfree(sense_data);
  487. return retval;
  488. }
  489. /**
  490. * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
  491. * @sg_list: structure describing a scatter gather list
  492. * @size: size of scatter-gather list
  493. * Return: 0 on success, else -ENOMEM
  494. *
  495. * In sg_list->sg a pointer to the created scatter-gather list is returned,
  496. * or NULL if we run out of memory. sg_list->count specifies the number of
  497. * elements of the scatter-gather list. The maximum size of a single element
  498. * in the scatter-gather list is PAGE_SIZE.
  499. */
  500. static inline int
  501. zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
  502. {
  503. struct scatterlist *sg;
  504. unsigned int i;
  505. int retval = 0;
  506. void *address;
  507. BUG_ON(sg_list == NULL);
  508. sg_list->count = size >> PAGE_SHIFT;
  509. if (size & ~PAGE_MASK)
  510. sg_list->count++;
  511. sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
  512. GFP_KERNEL);
  513. if (sg_list->sg == NULL) {
  514. sg_list->count = 0;
  515. retval = -ENOMEM;
  516. goto out;
  517. }
  518. for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
  519. sg->length = min(size, PAGE_SIZE);
  520. sg->offset = 0;
  521. address = (void *) get_zeroed_page(GFP_KERNEL);
  522. if (address == NULL) {
  523. sg_list->count = i;
  524. zfcp_sg_list_free(sg_list);
  525. retval = -ENOMEM;
  526. goto out;
  527. }
  528. zfcp_address_to_sg(address, sg);
  529. size -= sg->length;
  530. }
  531. out:
  532. return retval;
  533. }
  534. /**
  535. * zfcp_sg_list_free - free memory of a scatter-gather list
  536. * @sg_list: structure describing a scatter-gather list
  537. *
  538. * Memory for each element in the scatter-gather list is freed.
  539. * Finally sg_list->sg is freed itself and sg_list->count is reset.
  540. */
  541. static inline void
  542. zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
  543. {
  544. struct scatterlist *sg;
  545. unsigned int i;
  546. BUG_ON(sg_list == NULL);
  547. for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
  548. free_page((unsigned long) zfcp_sg_to_address(sg));
  549. sg_list->count = 0;
  550. kfree(sg_list->sg);
  551. }
  552. /**
  553. * zfcp_sg_size - determine size of a scatter-gather list
  554. * @sg: array of (struct scatterlist)
  555. * @sg_count: elements in array
  556. * Return: size of entire scatter-gather list
  557. */
  558. size_t
  559. zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
  560. {
  561. unsigned int i;
  562. struct scatterlist *p;
  563. size_t size;
  564. size = 0;
  565. for (i = 0, p = sg; i < sg_count; i++, p++) {
  566. BUG_ON(p == NULL);
  567. size += p->length;
  568. }
  569. return size;
  570. }
  571. /**
  572. * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
  573. * @sg_list: structure describing a scatter-gather list
  574. * @user_buffer: pointer to buffer in user space
  575. * @size: number of bytes to be copied
  576. * Return: 0 on success, -EFAULT if copy_from_user fails.
  577. */
  578. static inline int
  579. zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
  580. void __user *user_buffer,
  581. size_t size)
  582. {
  583. struct scatterlist *sg;
  584. unsigned int length;
  585. void *zfcp_buffer;
  586. int retval = 0;
  587. BUG_ON(sg_list == NULL);
  588. if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
  589. return -EFAULT;
  590. for (sg = sg_list->sg; size > 0; sg++) {
  591. length = min((unsigned int)size, sg->length);
  592. zfcp_buffer = zfcp_sg_to_address(sg);
  593. if (copy_from_user(zfcp_buffer, user_buffer, length)) {
  594. retval = -EFAULT;
  595. goto out;
  596. }
  597. user_buffer += length;
  598. size -= length;
  599. }
  600. out:
  601. return retval;
  602. }
  603. /**
  604. * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
  605. * @user_buffer: pointer to buffer in user space
  606. * @sg_list: structure describing a scatter-gather list
  607. * @size: number of bytes to be copied
  608. * Return: 0 on success, -EFAULT if copy_to_user fails
  609. */
  610. static inline int
  611. zfcp_sg_list_copy_to_user(void __user *user_buffer,
  612. struct zfcp_sg_list *sg_list,
  613. size_t size)
  614. {
  615. struct scatterlist *sg;
  616. unsigned int length;
  617. void *zfcp_buffer;
  618. int retval = 0;
  619. BUG_ON(sg_list == NULL);
  620. if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
  621. return -EFAULT;
  622. for (sg = sg_list->sg; size > 0; sg++) {
  623. length = min((unsigned int) size, sg->length);
  624. zfcp_buffer = zfcp_sg_to_address(sg);
  625. if (copy_to_user(user_buffer, zfcp_buffer, length)) {
  626. retval = -EFAULT;
  627. goto out;
  628. }
  629. user_buffer += length;
  630. size -= length;
  631. }
  632. out:
  633. return retval;
  634. }
  635. #undef ZFCP_LOG_AREA
  636. /****************************************************************/
  637. /****** Functions for configuration/set-up of structures ********/
  638. /****************************************************************/
  639. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
  640. /**
  641. * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
  642. * @port: pointer to port to search for unit
  643. * @fcp_lun: FCP LUN to search for
  644. * Traverse list of all units of a port and return pointer to a unit
  645. * with the given FCP LUN.
  646. */
  647. struct zfcp_unit *
  648. zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
  649. {
  650. struct zfcp_unit *unit;
  651. int found = 0;
  652. list_for_each_entry(unit, &port->unit_list_head, list) {
  653. if ((unit->fcp_lun == fcp_lun) &&
  654. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
  655. {
  656. found = 1;
  657. break;
  658. }
  659. }
  660. return found ? unit : NULL;
  661. }
  662. /**
  663. * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
  664. * @adapter: pointer to adapter to search for port
  665. * @wwpn: wwpn to search for
  666. * Traverse list of all ports of an adapter and return pointer to a port
  667. * with the given wwpn.
  668. */
  669. struct zfcp_port *
  670. zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
  671. {
  672. struct zfcp_port *port;
  673. int found = 0;
  674. list_for_each_entry(port, &adapter->port_list_head, list) {
  675. if ((port->wwpn == wwpn) &&
  676. !(atomic_read(&port->status) &
  677. (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
  678. found = 1;
  679. break;
  680. }
  681. }
  682. return found ? port : NULL;
  683. }
  684. /**
  685. * zfcp_get_port_by_did - find port in port list of adapter by d_id
  686. * @adapter: pointer to adapter to search for port
  687. * @d_id: d_id to search for
  688. * Traverse list of all ports of an adapter and return pointer to a port
  689. * with the given d_id.
  690. */
  691. struct zfcp_port *
  692. zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
  693. {
  694. struct zfcp_port *port;
  695. int found = 0;
  696. list_for_each_entry(port, &adapter->port_list_head, list) {
  697. if ((port->d_id == d_id) &&
  698. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
  699. {
  700. found = 1;
  701. break;
  702. }
  703. }
  704. return found ? port : NULL;
  705. }
  706. /**
  707. * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
  708. * @bus_id: bus_id to search for
  709. * Traverse list of all adapters and return pointer to an adapter
  710. * with the given bus_id.
  711. */
  712. struct zfcp_adapter *
  713. zfcp_get_adapter_by_busid(char *bus_id)
  714. {
  715. struct zfcp_adapter *adapter;
  716. int found = 0;
  717. list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
  718. if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
  719. BUS_ID_SIZE) == 0) &&
  720. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
  721. &adapter->status)){
  722. found = 1;
  723. break;
  724. }
  725. }
  726. return found ? adapter : NULL;
  727. }
  728. /**
  729. * zfcp_unit_enqueue - enqueue unit to unit list of a port.
  730. * @port: pointer to port where unit is added
  731. * @fcp_lun: FCP LUN of unit to be enqueued
  732. * Return: pointer to enqueued unit on success, NULL on error
  733. * Locks: config_sema must be held to serialize changes to the unit list
  734. *
  735. * Sets up some unit internal structures and creates sysfs entry.
  736. */
  737. struct zfcp_unit *
  738. zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
  739. {
  740. struct zfcp_unit *unit, *tmp_unit;
  741. unsigned int scsi_lun;
  742. int found;
  743. /*
  744. * check that there is no unit with this FCP_LUN already in list
  745. * and enqueue it.
  746. * Note: Unlike for the adapter and the port, this is an error
  747. */
  748. read_lock_irq(&zfcp_data.config_lock);
  749. unit = zfcp_get_unit_by_lun(port, fcp_lun);
  750. read_unlock_irq(&zfcp_data.config_lock);
  751. if (unit)
  752. return NULL;
  753. unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
  754. if (!unit)
  755. return NULL;
  756. /* initialise reference count stuff */
  757. atomic_set(&unit->refcount, 0);
  758. init_waitqueue_head(&unit->remove_wq);
  759. unit->port = port;
  760. unit->fcp_lun = fcp_lun;
  761. /* setup for sysfs registration */
  762. snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
  763. unit->sysfs_device.parent = &port->sysfs_device;
  764. unit->sysfs_device.release = zfcp_sysfs_unit_release;
  765. dev_set_drvdata(&unit->sysfs_device, unit);
  766. /* mark unit unusable as long as sysfs registration is not complete */
  767. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  768. if (device_register(&unit->sysfs_device)) {
  769. kfree(unit);
  770. return NULL;
  771. }
  772. if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
  773. device_unregister(&unit->sysfs_device);
  774. return NULL;
  775. }
  776. zfcp_unit_get(unit);
  777. scsi_lun = 0;
  778. found = 0;
  779. write_lock_irq(&zfcp_data.config_lock);
  780. list_for_each_entry(tmp_unit, &port->unit_list_head, list) {
  781. if (tmp_unit->scsi_lun != scsi_lun) {
  782. found = 1;
  783. break;
  784. }
  785. scsi_lun++;
  786. }
  787. unit->scsi_lun = scsi_lun;
  788. if (found)
  789. list_add_tail(&unit->list, &tmp_unit->list);
  790. else
  791. list_add_tail(&unit->list, &port->unit_list_head);
  792. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  793. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
  794. write_unlock_irq(&zfcp_data.config_lock);
  795. port->units++;
  796. zfcp_port_get(port);
  797. return unit;
  798. }
  799. void
  800. zfcp_unit_dequeue(struct zfcp_unit *unit)
  801. {
  802. zfcp_unit_wait(unit);
  803. write_lock_irq(&zfcp_data.config_lock);
  804. list_del(&unit->list);
  805. write_unlock_irq(&zfcp_data.config_lock);
  806. unit->port->units--;
  807. zfcp_port_put(unit->port);
  808. zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
  809. device_unregister(&unit->sysfs_device);
  810. }
  811. /*
  812. * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
  813. * commands.
  814. * It also genrates fcp-nameserver request/response buffer and unsolicited
  815. * status read fsf_req buffers.
  816. *
  817. * locks: must only be called with zfcp_data.config_sema taken
  818. */
  819. static int
  820. zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
  821. {
  822. adapter->pool.fsf_req_erp =
  823. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
  824. zfcp_data.fsf_req_qtcb_cache);
  825. if (!adapter->pool.fsf_req_erp)
  826. return -ENOMEM;
  827. adapter->pool.fsf_req_scsi =
  828. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
  829. zfcp_data.fsf_req_qtcb_cache);
  830. if (!adapter->pool.fsf_req_scsi)
  831. return -ENOMEM;
  832. adapter->pool.fsf_req_abort =
  833. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
  834. zfcp_data.fsf_req_qtcb_cache);
  835. if (!adapter->pool.fsf_req_abort)
  836. return -ENOMEM;
  837. adapter->pool.fsf_req_status_read =
  838. mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
  839. sizeof(struct zfcp_fsf_req));
  840. if (!adapter->pool.fsf_req_status_read)
  841. return -ENOMEM;
  842. adapter->pool.data_status_read =
  843. mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
  844. zfcp_data.sr_buffer_cache);
  845. if (!adapter->pool.data_status_read)
  846. return -ENOMEM;
  847. adapter->pool.data_gid_pn =
  848. mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
  849. zfcp_data.gid_pn_cache);
  850. if (!adapter->pool.data_gid_pn)
  851. return -ENOMEM;
  852. return 0;
  853. }
  854. /**
  855. * zfcp_free_low_mem_buffers - free memory pools of an adapter
  856. * @adapter: pointer to zfcp_adapter for which memory pools should be freed
  857. * locking: zfcp_data.config_sema must be held
  858. */
  859. static void
  860. zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
  861. {
  862. if (adapter->pool.fsf_req_erp)
  863. mempool_destroy(adapter->pool.fsf_req_erp);
  864. if (adapter->pool.fsf_req_scsi)
  865. mempool_destroy(adapter->pool.fsf_req_scsi);
  866. if (adapter->pool.fsf_req_abort)
  867. mempool_destroy(adapter->pool.fsf_req_abort);
  868. if (adapter->pool.fsf_req_status_read)
  869. mempool_destroy(adapter->pool.fsf_req_status_read);
  870. if (adapter->pool.data_status_read)
  871. mempool_destroy(adapter->pool.data_status_read);
  872. if (adapter->pool.data_gid_pn)
  873. mempool_destroy(adapter->pool.data_gid_pn);
  874. }
  875. void
  876. zfcp_dummy_release(struct device *dev)
  877. {
  878. return;
  879. }
  880. /*
  881. * Enqueues an adapter at the end of the adapter list in the driver data.
  882. * All adapter internal structures are set up.
  883. * Proc-fs entries are also created.
  884. *
  885. * returns: 0 if a new adapter was successfully enqueued
  886. * ZFCP_KNOWN if an adapter with this devno was already present
  887. * -ENOMEM if alloc failed
  888. * locks: config_sema must be held to serialise changes to the adapter list
  889. */
  890. struct zfcp_adapter *
  891. zfcp_adapter_enqueue(struct ccw_device *ccw_device)
  892. {
  893. int retval = 0;
  894. struct zfcp_adapter *adapter;
  895. /*
  896. * Note: It is safe to release the list_lock, as any list changes
  897. * are protected by the config_sema, which must be held to get here
  898. */
  899. /* try to allocate new adapter data structure (zeroed) */
  900. adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
  901. if (!adapter) {
  902. ZFCP_LOG_INFO("error: allocation of base adapter "
  903. "structure failed\n");
  904. goto out;
  905. }
  906. ccw_device->handler = NULL;
  907. /* save ccw_device pointer */
  908. adapter->ccw_device = ccw_device;
  909. retval = zfcp_qdio_allocate_queues(adapter);
  910. if (retval)
  911. goto queues_alloc_failed;
  912. retval = zfcp_qdio_allocate(adapter);
  913. if (retval)
  914. goto qdio_allocate_failed;
  915. retval = zfcp_allocate_low_mem_buffers(adapter);
  916. if (retval) {
  917. ZFCP_LOG_INFO("error: pool allocation failed\n");
  918. goto failed_low_mem_buffers;
  919. }
  920. /* initialise reference count stuff */
  921. atomic_set(&adapter->refcount, 0);
  922. init_waitqueue_head(&adapter->remove_wq);
  923. /* initialise list of ports */
  924. INIT_LIST_HEAD(&adapter->port_list_head);
  925. /* initialise list of ports to be removed */
  926. INIT_LIST_HEAD(&adapter->port_remove_lh);
  927. /* initialize list of fsf requests */
  928. spin_lock_init(&adapter->req_list_lock);
  929. retval = zfcp_reqlist_init(adapter);
  930. if (retval) {
  931. ZFCP_LOG_INFO("request list initialization failed\n");
  932. goto failed_low_mem_buffers;
  933. }
  934. /* initialize debug locks */
  935. spin_lock_init(&adapter->erp_dbf_lock);
  936. spin_lock_init(&adapter->hba_dbf_lock);
  937. spin_lock_init(&adapter->san_dbf_lock);
  938. spin_lock_init(&adapter->scsi_dbf_lock);
  939. /* initialize error recovery stuff */
  940. rwlock_init(&adapter->erp_lock);
  941. sema_init(&adapter->erp_ready_sem, 0);
  942. INIT_LIST_HEAD(&adapter->erp_ready_head);
  943. INIT_LIST_HEAD(&adapter->erp_running_head);
  944. /* initialize abort lock */
  945. rwlock_init(&adapter->abort_lock);
  946. /* initialise some erp stuff */
  947. init_waitqueue_head(&adapter->erp_thread_wqh);
  948. init_waitqueue_head(&adapter->erp_done_wqh);
  949. /* initialize lock of associated request queue */
  950. rwlock_init(&adapter->request_queue.queue_lock);
  951. /* intitialise SCSI ER timer */
  952. init_timer(&adapter->scsi_er_timer);
  953. /* mark adapter unusable as long as sysfs registration is not complete */
  954. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  955. adapter->ccw_device = ccw_device;
  956. dev_set_drvdata(&ccw_device->dev, adapter);
  957. if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
  958. goto sysfs_failed;
  959. adapter->generic_services.parent = &adapter->ccw_device->dev;
  960. adapter->generic_services.release = zfcp_dummy_release;
  961. snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
  962. "generic_services");
  963. if (device_register(&adapter->generic_services))
  964. goto generic_services_failed;
  965. /* put allocated adapter at list tail */
  966. write_lock_irq(&zfcp_data.config_lock);
  967. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  968. list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
  969. write_unlock_irq(&zfcp_data.config_lock);
  970. zfcp_data.adapters++;
  971. goto out;
  972. generic_services_failed:
  973. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  974. sysfs_failed:
  975. dev_set_drvdata(&ccw_device->dev, NULL);
  976. failed_low_mem_buffers:
  977. zfcp_free_low_mem_buffers(adapter);
  978. if (qdio_free(ccw_device) != 0)
  979. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  980. zfcp_get_busid_by_adapter(adapter));
  981. qdio_allocate_failed:
  982. zfcp_qdio_free_queues(adapter);
  983. queues_alloc_failed:
  984. kfree(adapter);
  985. adapter = NULL;
  986. out:
  987. return adapter;
  988. }
  989. /*
  990. * returns: 0 - struct zfcp_adapter data structure successfully removed
  991. * !0 - struct zfcp_adapter data structure could not be removed
  992. * (e.g. still used)
  993. * locks: adapter list write lock is assumed to be held by caller
  994. */
  995. void
  996. zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
  997. {
  998. int retval = 0;
  999. unsigned long flags;
  1000. device_unregister(&adapter->generic_services);
  1001. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  1002. dev_set_drvdata(&adapter->ccw_device->dev, NULL);
  1003. /* sanity check: no pending FSF requests */
  1004. spin_lock_irqsave(&adapter->req_list_lock, flags);
  1005. retval = zfcp_reqlist_isempty(adapter);
  1006. spin_unlock_irqrestore(&adapter->req_list_lock, flags);
  1007. if (!retval) {
  1008. ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
  1009. "%i requests outstanding\n",
  1010. zfcp_get_busid_by_adapter(adapter), adapter,
  1011. atomic_read(&adapter->reqs_active));
  1012. retval = -EBUSY;
  1013. goto out;
  1014. }
  1015. /* remove specified adapter data structure from list */
  1016. write_lock_irq(&zfcp_data.config_lock);
  1017. list_del(&adapter->list);
  1018. write_unlock_irq(&zfcp_data.config_lock);
  1019. /* decrease number of adapters in list */
  1020. zfcp_data.adapters--;
  1021. ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
  1022. "%i adapters still in list\n",
  1023. zfcp_get_busid_by_adapter(adapter),
  1024. adapter, zfcp_data.adapters);
  1025. retval = qdio_free(adapter->ccw_device);
  1026. if (retval)
  1027. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  1028. zfcp_get_busid_by_adapter(adapter));
  1029. zfcp_free_low_mem_buffers(adapter);
  1030. /* free memory of adapter data structure and queues */
  1031. zfcp_qdio_free_queues(adapter);
  1032. zfcp_reqlist_free(adapter);
  1033. kfree(adapter->fc_stats);
  1034. kfree(adapter->stats_reset_data);
  1035. ZFCP_LOG_TRACE("freeing adapter structure\n");
  1036. kfree(adapter);
  1037. out:
  1038. return;
  1039. }
  1040. /**
  1041. * zfcp_port_enqueue - enqueue port to port list of adapter
  1042. * @adapter: adapter where remote port is added
  1043. * @wwpn: WWPN of the remote port to be enqueued
  1044. * @status: initial status for the port
  1045. * @d_id: destination id of the remote port to be enqueued
  1046. * Return: pointer to enqueued port on success, NULL on error
  1047. * Locks: config_sema must be held to serialize changes to the port list
  1048. *
  1049. * All port internal structures are set up and the sysfs entry is generated.
  1050. * d_id is used to enqueue ports with a well known address like the Directory
  1051. * Service for nameserver lookup.
  1052. */
  1053. struct zfcp_port *
  1054. zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
  1055. u32 d_id)
  1056. {
  1057. struct zfcp_port *port;
  1058. int check_wwpn;
  1059. check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
  1060. /*
  1061. * check that there is no port with this WWPN already in list
  1062. */
  1063. if (check_wwpn) {
  1064. read_lock_irq(&zfcp_data.config_lock);
  1065. port = zfcp_get_port_by_wwpn(adapter, wwpn);
  1066. read_unlock_irq(&zfcp_data.config_lock);
  1067. if (port)
  1068. return NULL;
  1069. }
  1070. port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
  1071. if (!port)
  1072. return NULL;
  1073. /* initialise reference count stuff */
  1074. atomic_set(&port->refcount, 0);
  1075. init_waitqueue_head(&port->remove_wq);
  1076. INIT_LIST_HEAD(&port->unit_list_head);
  1077. INIT_LIST_HEAD(&port->unit_remove_lh);
  1078. port->adapter = adapter;
  1079. if (check_wwpn)
  1080. port->wwpn = wwpn;
  1081. atomic_set_mask(status, &port->status);
  1082. /* setup for sysfs registration */
  1083. if (status & ZFCP_STATUS_PORT_WKA) {
  1084. switch (d_id) {
  1085. case ZFCP_DID_DIRECTORY_SERVICE:
  1086. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1087. "directory");
  1088. break;
  1089. case ZFCP_DID_MANAGEMENT_SERVICE:
  1090. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1091. "management");
  1092. break;
  1093. case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
  1094. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1095. "key_distribution");
  1096. break;
  1097. case ZFCP_DID_ALIAS_SERVICE:
  1098. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1099. "alias");
  1100. break;
  1101. case ZFCP_DID_TIME_SERVICE:
  1102. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1103. "time");
  1104. break;
  1105. default:
  1106. kfree(port);
  1107. return NULL;
  1108. }
  1109. port->d_id = d_id;
  1110. port->sysfs_device.parent = &adapter->generic_services;
  1111. } else {
  1112. snprintf(port->sysfs_device.bus_id,
  1113. BUS_ID_SIZE, "0x%016llx", wwpn);
  1114. port->sysfs_device.parent = &adapter->ccw_device->dev;
  1115. }
  1116. port->sysfs_device.release = zfcp_sysfs_port_release;
  1117. dev_set_drvdata(&port->sysfs_device, port);
  1118. /* mark port unusable as long as sysfs registration is not complete */
  1119. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  1120. if (device_register(&port->sysfs_device)) {
  1121. kfree(port);
  1122. return NULL;
  1123. }
  1124. if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
  1125. device_unregister(&port->sysfs_device);
  1126. return NULL;
  1127. }
  1128. zfcp_port_get(port);
  1129. write_lock_irq(&zfcp_data.config_lock);
  1130. list_add_tail(&port->list, &adapter->port_list_head);
  1131. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  1132. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
  1133. if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
  1134. if (!adapter->nameserver_port)
  1135. adapter->nameserver_port = port;
  1136. adapter->ports++;
  1137. write_unlock_irq(&zfcp_data.config_lock);
  1138. zfcp_adapter_get(adapter);
  1139. return port;
  1140. }
  1141. void
  1142. zfcp_port_dequeue(struct zfcp_port *port)
  1143. {
  1144. zfcp_port_wait(port);
  1145. write_lock_irq(&zfcp_data.config_lock);
  1146. list_del(&port->list);
  1147. port->adapter->ports--;
  1148. write_unlock_irq(&zfcp_data.config_lock);
  1149. if (port->rport)
  1150. fc_remote_port_delete(port->rport);
  1151. port->rport = NULL;
  1152. zfcp_adapter_put(port->adapter);
  1153. zfcp_sysfs_port_remove_files(&port->sysfs_device,
  1154. atomic_read(&port->status));
  1155. device_unregister(&port->sysfs_device);
  1156. }
  1157. /* Enqueues a nameserver port */
  1158. int
  1159. zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
  1160. {
  1161. struct zfcp_port *port;
  1162. port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
  1163. ZFCP_DID_DIRECTORY_SERVICE);
  1164. if (!port) {
  1165. ZFCP_LOG_INFO("error: enqueue of nameserver port for "
  1166. "adapter %s failed\n",
  1167. zfcp_get_busid_by_adapter(adapter));
  1168. return -ENXIO;
  1169. }
  1170. zfcp_port_put(port);
  1171. return 0;
  1172. }
  1173. #undef ZFCP_LOG_AREA
  1174. /****************************************************************/
  1175. /******* Fibre Channel Standard related Functions **************/
  1176. /****************************************************************/
  1177. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FC
  1178. void
  1179. zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter,
  1180. struct fsf_status_read_buffer *status_buffer)
  1181. {
  1182. struct fcp_rscn_head *fcp_rscn_head;
  1183. struct fcp_rscn_element *fcp_rscn_element;
  1184. struct zfcp_port *port;
  1185. u16 i;
  1186. u16 no_entries;
  1187. u32 range_mask;
  1188. unsigned long flags;
  1189. fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
  1190. fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
  1191. /* see FC-FS */
  1192. no_entries = (fcp_rscn_head->payload_len / 4);
  1193. for (i = 1; i < no_entries; i++) {
  1194. /* skip head and start with 1st element */
  1195. fcp_rscn_element++;
  1196. switch (fcp_rscn_element->addr_format) {
  1197. case ZFCP_PORT_ADDRESS:
  1198. range_mask = ZFCP_PORTS_RANGE_PORT;
  1199. break;
  1200. case ZFCP_AREA_ADDRESS:
  1201. range_mask = ZFCP_PORTS_RANGE_AREA;
  1202. break;
  1203. case ZFCP_DOMAIN_ADDRESS:
  1204. range_mask = ZFCP_PORTS_RANGE_DOMAIN;
  1205. break;
  1206. case ZFCP_FABRIC_ADDRESS:
  1207. range_mask = ZFCP_PORTS_RANGE_FABRIC;
  1208. break;
  1209. default:
  1210. ZFCP_LOG_INFO("incoming RSCN with unknown "
  1211. "address format\n");
  1212. continue;
  1213. }
  1214. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1215. list_for_each_entry(port, &adapter->port_list_head, list) {
  1216. if (atomic_test_mask
  1217. (ZFCP_STATUS_PORT_WKA, &port->status))
  1218. continue;
  1219. /* Do we know this port? If not skip it. */
  1220. if (!atomic_test_mask
  1221. (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
  1222. ZFCP_LOG_INFO("incoming RSCN, trying to open "
  1223. "port 0x%016Lx\n", port->wwpn);
  1224. zfcp_erp_port_reopen(port,
  1225. ZFCP_STATUS_COMMON_ERP_FAILED);
  1226. continue;
  1227. }
  1228. /*
  1229. * FIXME: race: d_id might being invalidated
  1230. * (...DID_DID reset)
  1231. */
  1232. if ((port->d_id & range_mask)
  1233. == (fcp_rscn_element->nport_did & range_mask)) {
  1234. ZFCP_LOG_TRACE("reopen did 0x%08x\n",
  1235. fcp_rscn_element->nport_did);
  1236. /*
  1237. * Unfortunately, an RSCN does not specify the
  1238. * type of change a target underwent. We assume
  1239. * that it makes sense to reopen the link.
  1240. * FIXME: Shall we try to find out more about
  1241. * the target and link state before closing it?
  1242. * How to accomplish this? (nameserver?)
  1243. * Where would such code be put in?
  1244. * (inside or outside erp)
  1245. */
  1246. ZFCP_LOG_INFO("incoming RSCN, trying to open "
  1247. "port 0x%016Lx\n", port->wwpn);
  1248. zfcp_test_link(port);
  1249. }
  1250. }
  1251. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1252. }
  1253. }
  1254. static void
  1255. zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter,
  1256. struct fsf_status_read_buffer *status_buffer)
  1257. {
  1258. struct fsf_plogi *els_plogi;
  1259. struct zfcp_port *port;
  1260. unsigned long flags;
  1261. els_plogi = (struct fsf_plogi *) status_buffer->payload;
  1262. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1263. list_for_each_entry(port, &adapter->port_list_head, list) {
  1264. if (port->wwpn == (*(wwn_t *) &els_plogi->serv_param.wwpn))
  1265. break;
  1266. }
  1267. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1268. if (!port || (port->wwpn != (*(wwn_t *) &els_plogi->serv_param.wwpn))) {
  1269. ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
  1270. "with d_id 0x%08x on adapter %s\n",
  1271. status_buffer->d_id,
  1272. zfcp_get_busid_by_adapter(adapter));
  1273. } else {
  1274. zfcp_erp_port_forced_reopen(port, 0);
  1275. }
  1276. }
  1277. static void
  1278. zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter,
  1279. struct fsf_status_read_buffer *status_buffer)
  1280. {
  1281. struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
  1282. struct zfcp_port *port;
  1283. unsigned long flags;
  1284. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1285. list_for_each_entry(port, &adapter->port_list_head, list) {
  1286. if (port->wwpn == els_logo->nport_wwpn)
  1287. break;
  1288. }
  1289. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1290. if (!port || (port->wwpn != els_logo->nport_wwpn)) {
  1291. ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
  1292. "with d_id 0x%08x on adapter %s\n",
  1293. status_buffer->d_id,
  1294. zfcp_get_busid_by_adapter(adapter));
  1295. } else {
  1296. zfcp_erp_port_forced_reopen(port, 0);
  1297. }
  1298. }
  1299. static void
  1300. zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
  1301. struct fsf_status_read_buffer *status_buffer)
  1302. {
  1303. ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
  1304. "for adapter %s\n", *(u32 *) (status_buffer->payload),
  1305. zfcp_get_busid_by_adapter(adapter));
  1306. }
  1307. void
  1308. zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
  1309. {
  1310. struct fsf_status_read_buffer *status_buffer;
  1311. u32 els_type;
  1312. struct zfcp_adapter *adapter;
  1313. status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
  1314. els_type = *(u32 *) (status_buffer->payload);
  1315. adapter = fsf_req->adapter;
  1316. zfcp_san_dbf_event_incoming_els(fsf_req);
  1317. if (els_type == LS_PLOGI)
  1318. zfcp_fsf_incoming_els_plogi(adapter, status_buffer);
  1319. else if (els_type == LS_LOGO)
  1320. zfcp_fsf_incoming_els_logo(adapter, status_buffer);
  1321. else if ((els_type & 0xffff0000) == LS_RSCN)
  1322. /* we are only concerned with the command, not the length */
  1323. zfcp_fsf_incoming_els_rscn(adapter, status_buffer);
  1324. else
  1325. zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
  1326. }
  1327. /**
  1328. * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
  1329. * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
  1330. * @pool: pointer to mempool_t if non-null memory pool is used for allocation
  1331. */
  1332. static int
  1333. zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
  1334. {
  1335. struct zfcp_gid_pn_data *data;
  1336. if (pool != NULL) {
  1337. data = mempool_alloc(pool, GFP_ATOMIC);
  1338. if (likely(data != NULL)) {
  1339. data->ct.pool = pool;
  1340. }
  1341. } else {
  1342. data = kmalloc(sizeof(struct zfcp_gid_pn_data), GFP_ATOMIC);
  1343. }
  1344. if (NULL == data)
  1345. return -ENOMEM;
  1346. memset(data, 0, sizeof(*data));
  1347. data->ct.req = &data->req;
  1348. data->ct.resp = &data->resp;
  1349. data->ct.req_count = data->ct.resp_count = 1;
  1350. zfcp_address_to_sg(&data->ct_iu_req, &data->req);
  1351. zfcp_address_to_sg(&data->ct_iu_resp, &data->resp);
  1352. data->req.length = sizeof(struct ct_iu_gid_pn_req);
  1353. data->resp.length = sizeof(struct ct_iu_gid_pn_resp);
  1354. *gid_pn = data;
  1355. return 0;
  1356. }
  1357. /**
  1358. * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
  1359. * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
  1360. */
  1361. static void
  1362. zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
  1363. {
  1364. if ((gid_pn->ct.pool != 0))
  1365. mempool_free(gid_pn, gid_pn->ct.pool);
  1366. else
  1367. kfree(gid_pn);
  1368. return;
  1369. }
  1370. /**
  1371. * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
  1372. * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
  1373. */
  1374. int
  1375. zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
  1376. {
  1377. int ret;
  1378. struct ct_iu_gid_pn_req *ct_iu_req;
  1379. struct zfcp_gid_pn_data *gid_pn;
  1380. struct zfcp_adapter *adapter = erp_action->adapter;
  1381. ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
  1382. if (ret < 0) {
  1383. ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
  1384. "request failed for adapter %s\n",
  1385. zfcp_get_busid_by_adapter(adapter));
  1386. goto out;
  1387. }
  1388. /* setup nameserver request */
  1389. ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
  1390. ct_iu_req->header.revision = ZFCP_CT_REVISION;
  1391. ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  1392. ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
  1393. ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
  1394. ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
  1395. ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
  1396. ct_iu_req->wwpn = erp_action->port->wwpn;
  1397. /* setup parameters for send generic command */
  1398. gid_pn->ct.port = adapter->nameserver_port;
  1399. gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
  1400. gid_pn->ct.handler_data = (unsigned long) gid_pn;
  1401. gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
  1402. gid_pn->ct.timer = &erp_action->timer;
  1403. gid_pn->port = erp_action->port;
  1404. ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
  1405. erp_action);
  1406. if (ret) {
  1407. ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
  1408. "failed for adapter %s\n",
  1409. zfcp_get_busid_by_adapter(adapter));
  1410. zfcp_gid_pn_buffers_free(gid_pn);
  1411. }
  1412. out:
  1413. return ret;
  1414. }
  1415. /**
  1416. * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
  1417. * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
  1418. */
  1419. static void zfcp_ns_gid_pn_handler(unsigned long data)
  1420. {
  1421. struct zfcp_port *port;
  1422. struct zfcp_send_ct *ct;
  1423. struct ct_iu_gid_pn_req *ct_iu_req;
  1424. struct ct_iu_gid_pn_resp *ct_iu_resp;
  1425. struct zfcp_gid_pn_data *gid_pn;
  1426. gid_pn = (struct zfcp_gid_pn_data *) data;
  1427. port = gid_pn->port;
  1428. ct = &gid_pn->ct;
  1429. ct_iu_req = zfcp_sg_to_address(ct->req);
  1430. ct_iu_resp = zfcp_sg_to_address(ct->resp);
  1431. if (ct->status != 0)
  1432. goto failed;
  1433. if (zfcp_check_ct_response(&ct_iu_resp->header)) {
  1434. /* FIXME: do we need some specific erp entry points */
  1435. atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
  1436. goto failed;
  1437. }
  1438. /* paranoia */
  1439. if (ct_iu_req->wwpn != port->wwpn) {
  1440. ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
  1441. "lookup does not match expected wwpn 0x%016Lx "
  1442. "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
  1443. zfcp_get_busid_by_port(port));
  1444. goto mismatch;
  1445. }
  1446. /* looks like a valid d_id */
  1447. port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
  1448. atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
  1449. ZFCP_LOG_DEBUG("adapter %s: wwpn=0x%016Lx ---> d_id=0x%08x\n",
  1450. zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
  1451. goto out;
  1452. mismatch:
  1453. ZFCP_LOG_DEBUG("CT IUs do not match:\n");
  1454. ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
  1455. sizeof(struct ct_iu_gid_pn_req));
  1456. ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
  1457. sizeof(struct ct_iu_gid_pn_resp));
  1458. failed:
  1459. ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
  1460. "0x%016Lx for adapter %s\n",
  1461. port->wwpn, zfcp_get_busid_by_port(port));
  1462. out:
  1463. zfcp_gid_pn_buffers_free(gid_pn);
  1464. return;
  1465. }
  1466. /* reject CT_IU reason codes acc. to FC-GS-4 */
  1467. static const struct zfcp_rc_entry zfcp_ct_rc[] = {
  1468. {0x01, "invalid command code"},
  1469. {0x02, "invalid version level"},
  1470. {0x03, "logical error"},
  1471. {0x04, "invalid CT_IU size"},
  1472. {0x05, "logical busy"},
  1473. {0x07, "protocol error"},
  1474. {0x09, "unable to perform command request"},
  1475. {0x0b, "command not supported"},
  1476. {0x0d, "server not available"},
  1477. {0x0e, "session could not be established"},
  1478. {0xff, "vendor specific error"},
  1479. {0, NULL},
  1480. };
  1481. /* LS_RJT reason codes acc. to FC-FS */
  1482. static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
  1483. {0x01, "invalid LS_Command code"},
  1484. {0x03, "logical error"},
  1485. {0x05, "logical busy"},
  1486. {0x07, "protocol error"},
  1487. {0x09, "unable to perform command request"},
  1488. {0x0b, "command not supported"},
  1489. {0x0e, "command already in progress"},
  1490. {0xff, "vendor specific error"},
  1491. {0, NULL},
  1492. };
  1493. /* reject reason codes according to FC-PH/FC-FS */
  1494. static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
  1495. {0x01, "invalid D_ID"},
  1496. {0x02, "invalid S_ID"},
  1497. {0x03, "Nx_Port not available, temporary"},
  1498. {0x04, "Nx_Port not available, permament"},
  1499. {0x05, "class not supported"},
  1500. {0x06, "delimiter usage error"},
  1501. {0x07, "TYPE not supported"},
  1502. {0x08, "invalid Link_Control"},
  1503. {0x09, "invalid R_CTL field"},
  1504. {0x0a, "invalid F_CTL field"},
  1505. {0x0b, "invalid OX_ID"},
  1506. {0x0c, "invalid RX_ID"},
  1507. {0x0d, "invalid SEQ_ID"},
  1508. {0x0e, "invalid DF_CTL"},
  1509. {0x0f, "invalid SEQ_CNT"},
  1510. {0x10, "invalid parameter field"},
  1511. {0x11, "exchange error"},
  1512. {0x12, "protocol error"},
  1513. {0x13, "incorrect length"},
  1514. {0x14, "unsupported ACK"},
  1515. {0x15, "class of service not supported by entity at FFFFFE"},
  1516. {0x16, "login required"},
  1517. {0x17, "excessive sequences attempted"},
  1518. {0x18, "unable to establish exchange"},
  1519. {0x1a, "fabric path not available"},
  1520. {0x1b, "invalid VC_ID (class 4)"},
  1521. {0x1c, "invalid CS_CTL field"},
  1522. {0x1d, "insufficient resources for VC (class 4)"},
  1523. {0x1f, "invalid class of service"},
  1524. {0x20, "preemption request rejected"},
  1525. {0x21, "preemption not enabled"},
  1526. {0x22, "multicast error"},
  1527. {0x23, "multicast error terminate"},
  1528. {0x24, "process login required"},
  1529. {0xff, "vendor specific reject"},
  1530. {0, NULL},
  1531. };
  1532. /**
  1533. * zfcp_rc_description - return description for given reaon code
  1534. * @code: reason code
  1535. * @rc_table: table of reason codes and descriptions
  1536. */
  1537. static inline const char *
  1538. zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
  1539. {
  1540. const char *descr = "unknown reason code";
  1541. do {
  1542. if (code == rc_table->code) {
  1543. descr = rc_table->description;
  1544. break;
  1545. }
  1546. rc_table++;
  1547. } while (rc_table->code && rc_table->description);
  1548. return descr;
  1549. }
  1550. /**
  1551. * zfcp_check_ct_response - evaluate reason code for CT_IU
  1552. * @rjt: response payload to an CT_IU request
  1553. * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
  1554. */
  1555. int
  1556. zfcp_check_ct_response(struct ct_hdr *rjt)
  1557. {
  1558. if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
  1559. return 0;
  1560. if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
  1561. ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
  1562. "response code (0x%04hx)\n",
  1563. rjt->cmd_rsp_code);
  1564. return 1;
  1565. }
  1566. ZFCP_LOG_INFO("Generic Service command rejected\n");
  1567. ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
  1568. zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
  1569. (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
  1570. (u32) rjt->vendor_unique);
  1571. return 1;
  1572. }
  1573. /**
  1574. * zfcp_print_els_rjt - print reject parameter and description for ELS reject
  1575. * @rjt_par: reject parameter acc. to FC-PH/FC-FS
  1576. * @rc_table: table of reason codes and descriptions
  1577. */
  1578. static inline void
  1579. zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
  1580. const struct zfcp_rc_entry *rc_table)
  1581. {
  1582. ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
  1583. zfcp_rc_description(rjt_par->reason_code, rc_table),
  1584. (u32) rjt_par->action, (u32) rjt_par->reason_code,
  1585. (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
  1586. }
  1587. /**
  1588. * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
  1589. * @sq: status qualifier word
  1590. * @rjt_par: reject parameter as described in FC-PH and FC-FS
  1591. * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
  1592. */
  1593. int
  1594. zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
  1595. {
  1596. int ret = -EIO;
  1597. if (sq == FSF_IOSTAT_NPORT_RJT) {
  1598. ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
  1599. zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
  1600. /* invalid d_id */
  1601. if (rjt_par->reason_code == 0x01)
  1602. ret = -EREMCHG;
  1603. } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
  1604. ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
  1605. zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
  1606. /* invalid d_id */
  1607. if (rjt_par->reason_code == 0x01)
  1608. ret = -EREMCHG;
  1609. } else if (sq == FSF_IOSTAT_LS_RJT) {
  1610. ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
  1611. zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
  1612. ret = -EREMOTEIO;
  1613. } else
  1614. ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
  1615. return ret;
  1616. }
  1617. /**
  1618. * zfcp_plogi_evaluate - evaluate PLOGI playload and copy important fields
  1619. * into zfcp_port structure
  1620. * @port: zfcp_port structure
  1621. * @plogi: plogi payload
  1622. */
  1623. void
  1624. zfcp_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
  1625. {
  1626. port->maxframe_size = plogi->serv_param.common_serv_param[7] |
  1627. ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
  1628. if (plogi->serv_param.class1_serv_param[0] & 0x80)
  1629. port->supported_classes |= FC_COS_CLASS1;
  1630. if (plogi->serv_param.class2_serv_param[0] & 0x80)
  1631. port->supported_classes |= FC_COS_CLASS2;
  1632. if (plogi->serv_param.class3_serv_param[0] & 0x80)
  1633. port->supported_classes |= FC_COS_CLASS3;
  1634. if (plogi->serv_param.class4_serv_param[0] & 0x80)
  1635. port->supported_classes |= FC_COS_CLASS4;
  1636. }
  1637. #undef ZFCP_LOG_AREA