zfcp_scsi.c 27 KB

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