zfcp_scsi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  22. #include "zfcp_ext.h"
  23. #include <asm/atomic.h>
  24. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  25. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  26. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  27. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  28. void (*done) (struct scsi_cmnd *));
  29. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  30. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  31. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *);
  32. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  33. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  34. struct scsi_cmnd *);
  35. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
  36. unsigned int, unsigned int);
  37. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  38. struct zfcp_data zfcp_data = {
  39. .scsi_host_template = {
  40. .name = ZFCP_NAME,
  41. .module = THIS_MODULE,
  42. .proc_name = "zfcp",
  43. .slave_alloc = zfcp_scsi_slave_alloc,
  44. .slave_configure = zfcp_scsi_slave_configure,
  45. .slave_destroy = zfcp_scsi_slave_destroy,
  46. .queuecommand = zfcp_scsi_queuecommand,
  47. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  48. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  49. .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
  50. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  51. .can_queue = 4096,
  52. .this_id = -1,
  53. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  54. .cmd_per_lun = 1,
  55. .use_clustering = 1,
  56. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  57. .max_sectors = ZFCP_MAX_SECTORS,
  58. },
  59. .driver_version = ZFCP_VERSION,
  60. };
  61. /* Find start of Response Information in FCP response unit*/
  62. char *
  63. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  64. {
  65. char *fcp_rsp_info_ptr;
  66. fcp_rsp_info_ptr =
  67. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  68. return fcp_rsp_info_ptr;
  69. }
  70. /* Find start of Sense Information in FCP response unit*/
  71. char *
  72. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  73. {
  74. char *fcp_sns_info_ptr;
  75. fcp_sns_info_ptr =
  76. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  77. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  78. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  79. fcp_rsp_iu->fcp_rsp_len;
  80. return fcp_sns_info_ptr;
  81. }
  82. static fcp_dl_t *
  83. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  84. {
  85. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  86. fcp_dl_t *fcp_dl_addr;
  87. fcp_dl_addr = (fcp_dl_t *)
  88. ((unsigned char *) fcp_cmd +
  89. sizeof (struct fcp_cmnd_iu) + additional_length);
  90. /*
  91. * fcp_dl_addr = start address of fcp_cmnd structure +
  92. * size of fixed part + size of dynamically sized add_dcp_cdb field
  93. * SEE FCP-2 documentation
  94. */
  95. return fcp_dl_addr;
  96. }
  97. fcp_dl_t
  98. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  99. {
  100. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  101. }
  102. void
  103. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  104. {
  105. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  106. }
  107. /*
  108. * note: it's a bit-or operation not an assignment
  109. * regarding the specified byte
  110. */
  111. static inline void
  112. set_byte(int *result, char status, char pos)
  113. {
  114. *result |= status << (pos * 8);
  115. }
  116. void
  117. set_host_byte(int *result, char status)
  118. {
  119. set_byte(result, status, 2);
  120. }
  121. void
  122. set_driver_byte(int *result, char status)
  123. {
  124. set_byte(result, status, 3);
  125. }
  126. static int
  127. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  128. {
  129. struct zfcp_adapter *adapter;
  130. struct zfcp_unit *unit;
  131. unsigned long flags;
  132. int retval = -ENXIO;
  133. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  134. if (!adapter)
  135. goto out;
  136. read_lock_irqsave(&zfcp_data.config_lock, flags);
  137. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  138. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  139. &unit->status)) {
  140. sdp->hostdata = unit;
  141. unit->device = sdp;
  142. zfcp_unit_get(unit);
  143. retval = 0;
  144. }
  145. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  146. out:
  147. return retval;
  148. }
  149. /**
  150. * zfcp_scsi_slave_destroy - called when scsi device is removed
  151. *
  152. * Remove reference to associated scsi device for an zfcp_unit.
  153. * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
  154. * or a scan for this device might have failed.
  155. */
  156. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  157. {
  158. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  159. if (unit) {
  160. zfcp_erp_wait(unit->port->adapter);
  161. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  162. sdpnt->hostdata = NULL;
  163. unit->device = NULL;
  164. zfcp_erp_unit_failed(unit);
  165. zfcp_unit_put(unit);
  166. } else
  167. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  168. "address %p\n", sdpnt);
  169. }
  170. /*
  171. * called from scsi midlayer to allow finetuning of a device.
  172. */
  173. static int
  174. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  175. {
  176. if (sdp->tagged_supported)
  177. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  178. else
  179. scsi_adjust_queue_depth(sdp, 0, 1);
  180. return 0;
  181. }
  182. /**
  183. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  184. * @scpnt: pointer to struct scsi_cmnd where result is set
  185. * @result: result to be set in scpnt (e.g. DID_ERROR)
  186. */
  187. static void
  188. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  189. {
  190. set_host_byte(&scpnt->result, result);
  191. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  192. zfcp_scsi_dbf_event_result("fail", 4,
  193. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  194. scpnt, NULL);
  195. /* return directly */
  196. scpnt->scsi_done(scpnt);
  197. }
  198. /**
  199. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  200. * zfcp_scsi_command_sync
  201. * @adapter: adapter where scsi command is issued
  202. * @unit: unit to which scsi command is sent
  203. * @scpnt: scsi command to be sent
  204. * @timer: timer to be started if request is successfully initiated
  205. *
  206. * Note: In scsi_done function must be set in scpnt.
  207. */
  208. int
  209. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  210. struct scsi_cmnd *scpnt, int use_timer)
  211. {
  212. int tmp;
  213. int retval;
  214. retval = 0;
  215. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  216. BUG_ON(scpnt->scsi_done == NULL);
  217. if (unlikely(NULL == unit)) {
  218. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  219. goto out;
  220. }
  221. if (unlikely(
  222. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  223. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  224. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  225. "0x%016Lx on adapter %s\n",
  226. unit->fcp_lun, unit->port->wwpn,
  227. zfcp_get_busid_by_adapter(adapter));
  228. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  229. goto out;
  230. }
  231. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
  232. ZFCP_REQ_AUTO_CLEANUP);
  233. if (unlikely(tmp == -EBUSY)) {
  234. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  235. "on port 0x%016Lx in recovery\n",
  236. zfcp_get_busid_by_unit(unit),
  237. unit->fcp_lun, unit->port->wwpn);
  238. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  239. goto out;
  240. }
  241. if (unlikely(tmp < 0)) {
  242. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  243. retval = SCSI_MLQUEUE_HOST_BUSY;
  244. }
  245. out:
  246. return retval;
  247. }
  248. static void
  249. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  250. {
  251. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  252. complete(wait);
  253. }
  254. /**
  255. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  256. * @unit: unit where command is sent to
  257. * @scpnt: scsi command to be sent
  258. * @use_timer: indicates whether timer should be setup or not
  259. * Return: 0
  260. *
  261. * Errors are indicated in scpnt->result
  262. */
  263. int
  264. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  265. int use_timer)
  266. {
  267. int ret;
  268. DECLARE_COMPLETION_ONSTACK(wait);
  269. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  270. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  271. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
  272. use_timer);
  273. if (ret == 0)
  274. wait_for_completion(&wait);
  275. scpnt->SCp.ptr = NULL;
  276. return 0;
  277. }
  278. /*
  279. * function: zfcp_scsi_queuecommand
  280. *
  281. * purpose: enqueues a SCSI command to the specified target device
  282. *
  283. * returns: 0 - success, SCSI command enqueued
  284. * !0 - failure
  285. */
  286. static int
  287. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  288. void (*done) (struct scsi_cmnd *))
  289. {
  290. struct zfcp_unit *unit;
  291. struct zfcp_adapter *adapter;
  292. /* reset the status for this request */
  293. scpnt->result = 0;
  294. scpnt->host_scribble = NULL;
  295. scpnt->scsi_done = done;
  296. /*
  297. * figure out adapter and target device
  298. * (stored there by zfcp_scsi_slave_alloc)
  299. */
  300. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  301. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  302. return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
  303. }
  304. static struct zfcp_unit *
  305. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
  306. unsigned int lun)
  307. {
  308. struct zfcp_port *port;
  309. struct zfcp_unit *unit, *retval = NULL;
  310. list_for_each_entry(port, &adapter->port_list_head, list) {
  311. if (!port->rport || (id != port->rport->scsi_target_id))
  312. continue;
  313. list_for_each_entry(unit, &port->unit_list_head, list)
  314. if (lun == unit->scsi_lun) {
  315. retval = unit;
  316. goto out;
  317. }
  318. }
  319. out:
  320. return retval;
  321. }
  322. /**
  323. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  324. * @scpnt: pointer to scsi_cmnd to be aborted
  325. * Return: SUCCESS - command has been aborted and cleaned up in internal
  326. * bookkeeping, SCSI stack won't be called for aborted command
  327. * FAILED - otherwise
  328. *
  329. * We do not need to care for a SCSI command which completes normally
  330. * but late during this abort routine runs. We are allowed to return
  331. * late commands to the SCSI stack. It tracks the state of commands and
  332. * will handle late commands. (Usually, the normal completion of late
  333. * commands is ignored with respect to the running abort operation.)
  334. */
  335. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  336. {
  337. struct Scsi_Host *scsi_host;
  338. struct zfcp_adapter *adapter;
  339. struct zfcp_unit *unit;
  340. struct zfcp_fsf_req *fsf_req;
  341. unsigned long flags;
  342. unsigned long old_req_id;
  343. int retval = SUCCESS;
  344. scsi_host = scpnt->device->host;
  345. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  346. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  347. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  348. scpnt, zfcp_get_busid_by_adapter(adapter));
  349. /* avoid race condition between late normal completion and abort */
  350. write_lock_irqsave(&adapter->abort_lock, flags);
  351. /* Check whether corresponding fsf_req is still pending */
  352. spin_lock(&adapter->req_list_lock);
  353. fsf_req = zfcp_reqlist_find(adapter,
  354. (unsigned long) scpnt->host_scribble);
  355. spin_unlock(&adapter->req_list_lock);
  356. if (!fsf_req) {
  357. write_unlock_irqrestore(&adapter->abort_lock, flags);
  358. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
  359. retval = SUCCESS;
  360. goto out;
  361. }
  362. fsf_req->data = 0;
  363. fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  364. old_req_id = fsf_req->req_id;
  365. /* don't access old fsf_req after releasing the abort_lock */
  366. write_unlock_irqrestore(&adapter->abort_lock, flags);
  367. fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
  368. if (!fsf_req) {
  369. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  370. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  371. old_req_id);
  372. retval = FAILED;
  373. goto out;
  374. }
  375. __wait_event(fsf_req->completion_wq,
  376. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  377. if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  378. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
  379. retval = SUCCESS;
  380. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  381. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
  382. retval = SUCCESS;
  383. } else {
  384. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
  385. retval = FAILED;
  386. }
  387. zfcp_fsf_req_free(fsf_req);
  388. out:
  389. return retval;
  390. }
  391. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  392. {
  393. int retval;
  394. struct zfcp_unit *unit = scpnt->device->hostdata;
  395. if (!unit) {
  396. WARN_ON(1);
  397. return SUCCESS;
  398. }
  399. retval = zfcp_task_management_function(unit,
  400. FCP_LOGICAL_UNIT_RESET,
  401. scpnt);
  402. return retval ? FAILED : SUCCESS;
  403. }
  404. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
  405. {
  406. int retval;
  407. struct zfcp_unit *unit = scpnt->device->hostdata;
  408. if (!unit) {
  409. WARN_ON(1);
  410. return SUCCESS;
  411. }
  412. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  413. return retval ? FAILED : SUCCESS;
  414. }
  415. static int
  416. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  417. struct scsi_cmnd *scpnt)
  418. {
  419. struct zfcp_adapter *adapter = unit->port->adapter;
  420. struct zfcp_fsf_req *fsf_req;
  421. int retval = 0;
  422. /* issue task management function */
  423. fsf_req = zfcp_fsf_send_fcp_command_task_management
  424. (adapter, unit, tm_flags, 0);
  425. if (!fsf_req) {
  426. ZFCP_LOG_INFO("error: creation of task management request "
  427. "failed for unit 0x%016Lx on port 0x%016Lx on "
  428. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  429. zfcp_get_busid_by_adapter(adapter));
  430. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  431. retval = -ENOMEM;
  432. goto out;
  433. }
  434. __wait_event(fsf_req->completion_wq,
  435. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  436. /*
  437. * check completion status of task management function
  438. */
  439. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  440. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  441. retval = -EIO;
  442. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  443. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  444. retval = -ENOTSUPP;
  445. } else
  446. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  447. zfcp_fsf_req_free(fsf_req);
  448. out:
  449. return retval;
  450. }
  451. /**
  452. * zfcp_scsi_eh_host_reset_handler - handler for host reset
  453. */
  454. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  455. {
  456. struct zfcp_unit *unit;
  457. struct zfcp_adapter *adapter;
  458. unit = (struct zfcp_unit*) scpnt->device->hostdata;
  459. adapter = unit->port->adapter;
  460. ZFCP_LOG_NORMAL("host reset because of problems with "
  461. "unit 0x%016Lx on port 0x%016Lx, adapter %s\n",
  462. unit->fcp_lun, unit->port->wwpn,
  463. zfcp_get_busid_by_adapter(unit->port->adapter));
  464. zfcp_erp_adapter_reopen(adapter, 0);
  465. zfcp_erp_wait(adapter);
  466. return SUCCESS;
  467. }
  468. int
  469. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  470. {
  471. int retval = 0;
  472. static unsigned int unique_id = 0;
  473. if (adapter->scsi_host)
  474. goto out;
  475. /* register adapter as SCSI host with mid layer of SCSI stack */
  476. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  477. sizeof (struct zfcp_adapter *));
  478. if (!adapter->scsi_host) {
  479. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  480. "for adapter %s ",
  481. zfcp_get_busid_by_adapter(adapter));
  482. retval = -EIO;
  483. goto out;
  484. }
  485. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  486. /* tell the SCSI stack some characteristics of this adapter */
  487. adapter->scsi_host->max_id = 1;
  488. adapter->scsi_host->max_lun = 1;
  489. adapter->scsi_host->max_channel = 0;
  490. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  491. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  492. adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
  493. /*
  494. * save a pointer to our own adapter data structure within
  495. * hostdata field of SCSI host data structure
  496. */
  497. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  498. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  499. scsi_host_put(adapter->scsi_host);
  500. retval = -EIO;
  501. goto out;
  502. }
  503. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  504. out:
  505. return retval;
  506. }
  507. void
  508. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  509. {
  510. struct Scsi_Host *shost;
  511. struct zfcp_port *port;
  512. shost = adapter->scsi_host;
  513. if (!shost)
  514. return;
  515. read_lock_irq(&zfcp_data.config_lock);
  516. list_for_each_entry(port, &adapter->port_list_head, list)
  517. if (port->rport)
  518. port->rport = NULL;
  519. read_unlock_irq(&zfcp_data.config_lock);
  520. fc_remove_host(shost);
  521. scsi_remove_host(shost);
  522. scsi_host_put(shost);
  523. adapter->scsi_host = NULL;
  524. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  525. return;
  526. }
  527. /*
  528. * Support functions for FC transport class
  529. */
  530. static struct fc_host_statistics*
  531. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  532. {
  533. struct fc_host_statistics *fc_stats;
  534. if (!adapter->fc_stats) {
  535. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  536. if (!fc_stats)
  537. return NULL;
  538. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  539. }
  540. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  541. return adapter->fc_stats;
  542. }
  543. static void
  544. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  545. struct fsf_qtcb_bottom_port *data,
  546. struct fsf_qtcb_bottom_port *old)
  547. {
  548. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  549. old->seconds_since_last_reset;
  550. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  551. fc_stats->tx_words = data->tx_words - old->tx_words;
  552. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  553. fc_stats->rx_words = data->rx_words - old->rx_words;
  554. fc_stats->lip_count = data->lip - old->lip;
  555. fc_stats->nos_count = data->nos - old->nos;
  556. fc_stats->error_frames = data->error_frames - old->error_frames;
  557. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  558. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  559. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  560. fc_stats->loss_of_signal_count = data->loss_of_signal -
  561. old->loss_of_signal;
  562. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  563. old->psp_error_counts;
  564. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  565. old->invalid_tx_words;
  566. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  567. fc_stats->fcp_input_requests = data->input_requests -
  568. old->input_requests;
  569. fc_stats->fcp_output_requests = data->output_requests -
  570. old->output_requests;
  571. fc_stats->fcp_control_requests = data->control_requests -
  572. old->control_requests;
  573. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  574. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  575. }
  576. static void
  577. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  578. struct fsf_qtcb_bottom_port *data)
  579. {
  580. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  581. fc_stats->tx_frames = data->tx_frames;
  582. fc_stats->tx_words = data->tx_words;
  583. fc_stats->rx_frames = data->rx_frames;
  584. fc_stats->rx_words = data->rx_words;
  585. fc_stats->lip_count = data->lip;
  586. fc_stats->nos_count = data->nos;
  587. fc_stats->error_frames = data->error_frames;
  588. fc_stats->dumped_frames = data->dumped_frames;
  589. fc_stats->link_failure_count = data->link_failure;
  590. fc_stats->loss_of_sync_count = data->loss_of_sync;
  591. fc_stats->loss_of_signal_count = data->loss_of_signal;
  592. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  593. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  594. fc_stats->invalid_crc_count = data->invalid_crcs;
  595. fc_stats->fcp_input_requests = data->input_requests;
  596. fc_stats->fcp_output_requests = data->output_requests;
  597. fc_stats->fcp_control_requests = data->control_requests;
  598. fc_stats->fcp_input_megabytes = data->input_mb;
  599. fc_stats->fcp_output_megabytes = data->output_mb;
  600. }
  601. /**
  602. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  603. *
  604. * assumption: scsi_transport_fc synchronizes calls of
  605. * get_fc_host_stats and reset_fc_host_stats
  606. * (XXX to be checked otherwise introduce locking)
  607. */
  608. static struct fc_host_statistics *
  609. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  610. {
  611. struct zfcp_adapter *adapter;
  612. struct fc_host_statistics *fc_stats;
  613. struct fsf_qtcb_bottom_port *data;
  614. int ret;
  615. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  616. fc_stats = zfcp_init_fc_host_stats(adapter);
  617. if (!fc_stats)
  618. return NULL;
  619. data = kzalloc(sizeof(*data), GFP_KERNEL);
  620. if (!data)
  621. return NULL;
  622. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  623. if (ret) {
  624. kfree(data);
  625. return NULL; /* XXX return zeroed fc_stats? */
  626. }
  627. if (adapter->stats_reset &&
  628. ((jiffies/HZ - adapter->stats_reset) <
  629. data->seconds_since_last_reset)) {
  630. zfcp_adjust_fc_host_stats(fc_stats, data,
  631. adapter->stats_reset_data);
  632. } else
  633. zfcp_set_fc_host_stats(fc_stats, data);
  634. kfree(data);
  635. return fc_stats;
  636. }
  637. static void
  638. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  639. {
  640. struct zfcp_adapter *adapter;
  641. struct fsf_qtcb_bottom_port *data, *old_data;
  642. int ret;
  643. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  644. data = kzalloc(sizeof(*data), GFP_KERNEL);
  645. if (!data)
  646. return;
  647. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  648. if (ret) {
  649. kfree(data);
  650. } else {
  651. adapter->stats_reset = jiffies/HZ;
  652. old_data = adapter->stats_reset_data;
  653. adapter->stats_reset_data = data; /* finally freed in
  654. adater_dequeue */
  655. kfree(old_data);
  656. }
  657. }
  658. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  659. {
  660. rport->dev_loss_tmo = timeout;
  661. }
  662. struct fc_function_template zfcp_transport_functions = {
  663. .show_starget_port_id = 1,
  664. .show_starget_port_name = 1,
  665. .show_starget_node_name = 1,
  666. .show_rport_supported_classes = 1,
  667. .show_rport_maxframe_size = 1,
  668. .show_rport_dev_loss_tmo = 1,
  669. .show_host_node_name = 1,
  670. .show_host_port_name = 1,
  671. .show_host_permanent_port_name = 1,
  672. .show_host_supported_classes = 1,
  673. .show_host_supported_speeds = 1,
  674. .show_host_maxframe_size = 1,
  675. .show_host_serial_number = 1,
  676. .get_fc_host_stats = zfcp_get_fc_host_stats,
  677. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  678. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  679. /* no functions registered for following dynamic attributes but
  680. directly set by LLDD */
  681. .show_host_port_type = 1,
  682. .show_host_speed = 1,
  683. .show_host_port_id = 1,
  684. .disable_target_scan = 1,
  685. };
  686. /**
  687. * ZFCP_DEFINE_SCSI_ATTR
  688. * @_name: name of show attribute
  689. * @_format: format string
  690. * @_value: value to print
  691. *
  692. * Generates attribute for a unit.
  693. */
  694. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  695. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  696. char *buf) \
  697. { \
  698. struct scsi_device *sdev; \
  699. struct zfcp_unit *unit; \
  700. \
  701. sdev = to_scsi_device(dev); \
  702. unit = sdev->hostdata; \
  703. return sprintf(buf, _format, _value); \
  704. } \
  705. \
  706. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  707. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  708. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  709. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  710. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  711. &dev_attr_fcp_lun,
  712. &dev_attr_wwpn,
  713. &dev_attr_hba_id,
  714. NULL
  715. };
  716. #undef ZFCP_LOG_AREA