zfcp_aux.c 50 KB

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