zfcp_aux.c 51 KB

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