zfcp_aux.c 55 KB

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