zfcp_aux.c 50 KB

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